Skip to content

Commit

Permalink
chore(everything-else): convert everything else to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 11, 2019
1 parent 6a75f24 commit 904996d
Show file tree
Hide file tree
Showing 29 changed files with 1,314 additions and 906 deletions.
1 change: 1 addition & 0 deletions babel.config.js
@@ -1,5 +1,6 @@
'use strict';

// todo: https://github.com/babel/babel/issues/8529 :'(
module.exports = {
plugins: ['replace-ts-export-assignment'],
presets: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-alias-methods';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('no-alias-methods', rule, {
valid: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-truthy-falsy';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('no-truthy-falsy', rule, {
valid: [
Expand All @@ -28,6 +28,17 @@ ruleTester.run('no-truthy-falsy', rule, {
},
],
},
{
code: 'expect(true)["toBeTruthy"]();',
errors: [
{
messageId: 'avoidMessage',
data: { methodName: 'toBeTruthy' },
column: 14,
line: 1,
},
],
},
{
code: 'expect(false).not.toBeTruthy();',
errors: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-called-with';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('prefer-called-with', rule, {
valid: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-to-be-null';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('prefer-to-be-null', rule, {
valid: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-to-be-undefined';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('prefer-to-be-undefined', rule, {
valid: [
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-to-contain';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('prefer-to-contain', rule, {
valid: [
Expand Down Expand Up @@ -31,6 +31,12 @@ ruleTester.run('prefer-to-contain', rule, {
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
output: 'expect(a).toContain(b);',
},
// todo: support this, as it's counted by isSupportedAccessor
// {
// code: "expect(a['includes'](b)).toEqual(true);",
// errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
// output: 'expect(a).toContain(b);',
// },
{
code: 'expect(a.includes(b)).toEqual(false);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
Expand Down
@@ -1,19 +1,31 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-to-have-length';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 2015,
},
});

ruleTester.run('prefer-to-have-length', rule, {
valid: [
'expect(files).toHaveLength(1);',
"expect(files.name).toBe('file');",
"expect(files[`name`]).toBe('file');",
'expect(result).toBe(true);',
`expect(user.getUserName(5)).resolves.toEqual('Paul')`,
`expect(user.getUserName(5)).rejects.toEqual('Paul')`,
'expect(a);',
'expect(files["length"]).toBe(1);',
],

invalid: [
// todo: support this
// {
// code: 'expect(files["length"]).toBe(1);',
// errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
// output: 'expect(files).toHaveLength(1);',
// },
{
code: 'expect(files.length).toBe(1);',
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
Expand Down
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../require-tothrow-message';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 8,
},
Expand All @@ -12,55 +12,74 @@ ruleTester.run('require-tothrow-message', rule, {
// String
"expect(() => { throw new Error('a'); }).toThrow('a');",
"expect(() => { throw new Error('a'); }).toThrowError('a');",
`test('string', async () => {
`
test('string', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow('a');
await expect(throwErrorAsync()).rejects.toThrowError('a');
})`,
})
`,

// Template literal
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);",
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrowError(`${a}`);",
`test('Template literal', async () => {
`
test('Template literal', async () => {
const a = 'a';
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow(\`\${a}\`);
await expect(throwErrorAsync()).rejects.toThrowError(\`\${a}\`);
})`,
})
`,

// Regex
"expect(() => { throw new Error('a'); }).toThrow(/^a$/);",
"expect(() => { throw new Error('a'); }).toThrowError(/^a$/);",
`test('Regex', async () => {
`
test('Regex', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow(/^a$/);
await expect(throwErrorAsync()).rejects.toThrowError(/^a$/);
})`,
})
`,

// Function
"expect(() => { throw new Error('a'); })" +
".toThrow((() => { return 'a'; })());",
"expect(() => { throw new Error('a'); })" +
".toThrowError((() => { return 'a'; })());",
`test('Function', async () => {
"expect(() => { throw new Error('a'); }).toThrow((() => { return 'a'; })());",
"expect(() => { throw new Error('a'); }).toThrowError((() => { return 'a'; })());",
`
test('Function', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
const fn = () => { return 'a'; };
await expect(throwErrorAsync()).rejects.toThrow(fn());
await expect(throwErrorAsync()).rejects.toThrowError(fn());
})`,
})
`,

// Allow no message for `not`.
"expect(() => { throw new Error('a'); }).not.toThrow();",
"expect(() => { throw new Error('a'); }).not.toThrowError();",
`test('Allow no message for "not"', async () => {
`
test('Allow no message for "not"', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).resolves.not.toThrow();
await expect(throwErrorAsync()).resolves.not.toThrowError();
})`,
})
`,
'expect(a);',
],

invalid: [
{
code: "expect(() => { throw new Error('a'); })[`toThrow`]();",
errors: [
{
messageId: 'requireRethrow',
data: { propertyName: 'toThrow' },
column: 41,
line: 1,
},
],
},
// Empty toThrow
{
code: "expect(() => { throw new Error('a'); }).toThrow();",
Expand Down Expand Up @@ -88,23 +107,25 @@ ruleTester.run('require-tothrow-message', rule, {

// Empty rejects.toThrow / rejects.toThrowError
{
code: `test('empty rejects.toThrow', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow();
await expect(throwErrorAsync()).rejects.toThrowError();
})`,
code: `
test('empty rejects.toThrow', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow();
await expect(throwErrorAsync()).rejects.toThrowError();
})
`,
errors: [
{
messageId: 'requireRethrow',
data: { propertyName: 'toThrow' },
column: 49,
line: 3,
column: 51,
line: 4,
},
{
messageId: 'requireRethrow',
data: { propertyName: 'toThrowError' },
column: 49,
line: 4,
column: 51,
line: 5,
},
],
},
Expand Down

0 comments on commit 904996d

Please sign in to comment.