Skip to content

Commit

Permalink
[Tests] extract common "get parser" logic into test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 19, 2019
1 parent f4e3f1b commit 3b21de6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 82 deletions.
17 changes: 2 additions & 15 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

var ruleTester = new RuleTester()
, rule = require('rules/export')
Expand Down Expand Up @@ -111,18 +109,7 @@ ruleTester.run('export', rule, {


context('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
Expand Down
17 changes: 2 additions & 15 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve'

Expand Down Expand Up @@ -285,18 +283,7 @@ ruleTester.run('named (export *)', rule, {


context('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

if (semver.satisfies(eslintPkg.version, '<6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
['typescript', 'typescript-declare', 'typescript-export-assign'].forEach((source) => {
ruleTester.run(`named`, rule, {
valid: [
Expand Down
19 changes: 2 additions & 17 deletions tests/src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'


const ruleTester = new RuleTester()
, rule = require('rules/no-deprecated')
Expand Down Expand Up @@ -202,19 +199,7 @@ ruleTester.run('no-deprecated: hoisting', rule, {
})

describe('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

// typescript-eslint-parser doesn't support this rule on ESLint <4 for some reason
if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
Expand Down
23 changes: 3 additions & 20 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, testVersion } from '../utils'
import { test, testVersion, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'

Expand Down Expand Up @@ -1372,8 +1372,7 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur before import of `async`',
}],
})),
// fix incorrect order with typescript-eslint-parser
testVersion('<6.0.0', () => ({
...getTSParsers().map(parser => ({
code: `
var async = require('async');
var fs = require('fs');
Expand All @@ -1382,23 +1381,7 @@ ruleTester.run('order', rule, {
var fs = require('fs');
var async = require('async');
`,
parser: require.resolve('typescript-eslint-parser'),
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
})),
// fix incorrect order with @typescript-eslint/parser
testVersion('>5.0.0', () => ({
code: `
var async = require('async');
var fs = require('fs');
`,
output: `
var fs = require('fs');
var async = require('async');
`,
parser: require.resolve('@typescript-eslint/parser'),
parser,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
Expand Down
17 changes: 2 additions & 15 deletions tests/src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { test } from '../utils'
import { test, getNonDefaultParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

const ruleTester = new RuleTester()
, rule = require('rules/prefer-default-export')
Expand Down Expand Up @@ -133,18 +131,7 @@ ruleTester.run('prefer-default-export', rule, {
});

context('Typescript', function() {
// Typescript
const parsers = [require.resolve('babel-eslint')];

if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'));
}

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'));
}

parsers.forEach((parser) => {
getNonDefaultParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
Expand Down
16 changes: 16 additions & 0 deletions tests/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export function testFilePath(relativePath) {
return path.join(process.cwd(), './tests/files', relativePath)
}

export function getTSParsers() {
const parsers = [];
if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'));
}

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'));
}
return parsers;
}

export function getNonDefaultParsers() {
return getTSParsers().concat(require.resolve('babel-eslint'));
}

export const FILENAME = testFilePath('foo.js')

export function testVersion(specifier, t) {
Expand Down

0 comments on commit 3b21de6

Please sign in to comment.