inline-fixtures
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

inline-fixtures

CircleCI

Sometimes tests need fixture directories. Observe:

const a = require(path.join('module-require', 'relative', 'dot.js'));
const b = require(path.join('module-require', 'relative', 'dot-slash.js'));
//...

At this point it is not clear what the fixture files themselves actually do, or how the rest of the test file relates to them. One must have both the fixtures and the test open together to actually understand the test.

It would be nice if the fixtures were inline, next to the test source code.

inline-fixtures dynamically creates a temporary directory and populates it with the fixture layout you provide, and then calls the passed in function. This has the additional benefit that fixture mutation cannot leak from one test to another.

Example:

import {withFixtures} from 'inline-fixtures';
 
describe('tests for fs.readFileSync', () => {
  it('should have a test cases', async () => {
    const FIXTURES = {
      'README.md': 'Hello Mars.',
      anotherDir: {
        'index.js': '42;',
      },
    };
    await withFixtures(FIXTURES, async (fixturesDir) => {
      // A temporary `fixturesDir` exists at this point with `README.md`
      // and `anotherDir`. The latter contains `index.js`.
      const readmePath = path.join(fixturesDir, 'README.md');
      const contents = fs.readFileSync(readmePath, 'utf8');
      assert.strictEqual(contents, FIXTURES['README.md']);     
    });
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i inline-fixtures

Weekly Downloads

1,344

Version

1.1.0

License

Apache-2.0

Unpacked Size

21.4 kB

Total Files

9

Last publish

Collaborators

  • google-wombot
  • ofrobots