diff --git a/README.md b/README.md index 3ca514b..b0abc42 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,34 @@ fixtures('my plugin', path.join(__dirname, '__fixtures__'), { By default, it will compare the outputs with the files on the filesystem and you have to manually update the files in case of a mismatch. If you're using Jest, you can use the snapshot feature to automatically update the files with a keypress. ([See below](#integration-with-jest-snapshot)) on how to set it up. +### Customizing the test file name + +Sometimes, the name of the test file can be relevant to the test itself. In that case, the default name of `code.js` can be overridden by specifying a `babel-test.json` configuration file inside the fixture directory: + +```sh +. +├── function-expression +│ ├── code.js +│ └── output.js +├── invalid-syntax +│ ├── code.js +│ └── error.js +└── simple-variable + ├── babel-test.json + ├── input.js + └── output.js +``` + +The configuration file supports a single option: `inputFileName`, which specifies the name to use instead of `code.js`. + +```json +{ + "inputFileName": "input.js" +} +``` + +The name of `output.js` cannot be overridden. + ### Standalone test To run a standalone test with some custom logic, you can use the `test` function returned from `create`: diff --git a/src/__fixtures__/custom-file-name/babel-test.json b/src/__fixtures__/custom-file-name/babel-test.json new file mode 100644 index 0000000..5a284b5 --- /dev/null +++ b/src/__fixtures__/custom-file-name/babel-test.json @@ -0,0 +1,3 @@ +{ + "inputFileName": "input.js" +} diff --git a/src/__fixtures__/custom-file-name/input.js b/src/__fixtures__/custom-file-name/input.js new file mode 100644 index 0000000..9fac7f5 --- /dev/null +++ b/src/__fixtures__/custom-file-name/input.js @@ -0,0 +1 @@ +var title = 'hello world'; diff --git a/src/__fixtures__/custom-file-name/output.js b/src/__fixtures__/custom-file-name/output.js new file mode 100644 index 0000000..e2ab077 --- /dev/null +++ b/src/__fixtures__/custom-file-name/output.js @@ -0,0 +1 @@ +var eltit = 'hello world'; diff --git a/src/index.js b/src/index.js index cfe5f4f..eaf8107 100644 --- a/src/index.js +++ b/src/index.js @@ -78,7 +78,9 @@ exports.create = function create(config) { : it; t(f.replace(/^(skip|only)\./, '').replace(/(-|_)/g, ' '), () => { - const filename = path.join(path.join(directory, f), 'code.js'); + const testDirectory = path.join(directory, f); + const testConfig = loadTestConfig(testDirectory); + const filename = path.join(testDirectory, testConfig.inputFileName); const content = fs.readFileSync(filename, 'utf8'); return Promise.resolve(callback(content, { filename })).then( @@ -228,3 +230,17 @@ exports.create = function create(config) { return { test, fixtures }; }; + +function loadTestConfig(directory) { + const configFilename = path.join(directory, 'babel-test.json'); + const defaultConfig = { + inputFileName: 'code.js', + }; + + try { + const config = JSON.parse(fs.readFileSync(configFilename, 'utf8')); + return Object.assign(defaultConfig, config); + } catch (_) { + return defaultConfig; + } +}