Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the name of the input file in fixtures (overriding code.js) #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
3 changes: 3 additions & 0 deletions src/__fixtures__/custom-file-name/babel-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"inputFileName": "input.js"
}
1 change: 1 addition & 0 deletions src/__fixtures__/custom-file-name/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var title = 'hello world';
1 change: 1 addition & 0 deletions src/__fixtures__/custom-file-name/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var eltit = 'hello world';
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}