diff --git a/test/example/example.spec.ts b/test/example/example.spec.ts deleted file mode 100644 index 8e37228..0000000 --- a/test/example/example.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ - -import { describe } from 'mocha' -import * as path from 'path' -import testFixtures from '../testFixtures' -const example = require('../../src/example') - -describe(`example`, function() { - testFixtures({ - glob: path.join(__dirname, 'fixtures', '*.ts'), - transform: example, - }) -}) diff --git a/test/example/fixtures/fixture.ts b/test/example/fixture.ts similarity index 100% rename from test/example/fixtures/fixture.ts rename to test/example/fixture.ts diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000..ce1e60d --- /dev/null +++ b/test/index.spec.ts @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ + +import { describe } from 'mocha' +import * as fs from 'fs' +import * as path from 'path' +import testFixtures from './testFixtures' + +const dirs = fs.readdirSync(__dirname) +for (const dir of dirs) { + if (fs.statSync(path.join(__dirname, dir)).isDirectory()) { + describe(dir, function() { + testFixtures({ + glob: path.join(__dirname, dir, '*.ts'), + transform: require(`../src/${dir}`), + }) + }) + } +} diff --git a/test/testFixtures.ts b/test/testFixtures.ts index de5699d..cf24366 100644 --- a/test/testFixtures.ts +++ b/test/testFixtures.ts @@ -52,20 +52,23 @@ export default function textFixtures({ it(path.basename(fixturePath).replace(/\.js$/, ''), function() { let source = input const position = source.indexOf('// position') + let selectionStart + let selectionEnd if (position >= 0) { + selectionStart = selectionEnd = position source = source.replace(/^\s*\/\/ position[^\r\n]*(\r\n?|\n)/gm, '') - } - let selectionStart = source.indexOf('/* selectionStart */') - let selectionEnd - if (selectionStart >= 0) { - source = source.replace('/* selectionStart */', '') - selectionEnd = source.indexOf('/* selectionEnd */') - if (selectionEnd < 0) { - throw new Error( - '/* selectionEnd */ must be given if /* selectionStart */ is' - ) + } else { + selectionStart = source.indexOf('/* selectionStart */') + if (selectionStart >= 0) { + source = source.replace('/* selectionStart */', '') + selectionEnd = source.indexOf('/* selectionEnd */') + if (selectionEnd < 0) { + throw new Error( + '/* selectionEnd */ must be given if /* selectionStart */ is' + ) + } + source = source.replace('/* selectionEnd */', '') } - source = source.replace('/* selectionEnd */', '') } if (selectionStart < 0) selectionStart = position if (selectionEnd < 0) selectionEnd = position @@ -78,23 +81,29 @@ export default function textFixtures({ const report = [] const parser = fixture.parser || defaultParser const j = parser ? jscodeshift.withParser(parser) : jscodeshift - const result = transform( - { path: file, source }, - { - j, - jscodeshift: j, - stats: (name: string, quantity = 1): void => { - const total = stats[name] - stats[name] = total != null ? total + quantity : quantity + const doTransform = (): string | null | void | undefined => + transform( + { path: file, source }, + { + j, + jscodeshift: j, + stats: (name: string, quantity = 1): void => { + const total = stats[name] + stats[name] = total != null ? total + quantity : quantity + }, + report: (msg: string) => report.push(msg), }, - report: (msg: string) => report.push(msg), - }, - options - ) - if (!result) expect(result).to.equal(fixture.expected) - else expect(normalize(result)).to.equal(normalize(expected)) - if (fixture.stats) expect(stats).to.deep.equal(fixture.stats) - if (fixture.report) expect(report).to.deep.equal(fixture.report) + options + ) + if (fixture.expectedError) { + expect(doTransform).to.throw(fixture.expectedError) + } else { + const result = doTransform() + if (!result) expect(result).to.equal(fixture.expected) + else expect(normalize(result)).to.equal(normalize(expected)) + if (fixture.stats) expect(stats).to.deep.equal(fixture.stats) + if (fixture.report) expect(report).to.deep.equal(fixture.report) + } }) } }