Skip to content

Commit

Permalink
move dynamic test creation to ./utils.js (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored and cpojer committed Sep 22, 2016
1 parent 891b7be commit a5de858
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
37 changes: 9 additions & 28 deletions integration_tests/__tests__/toMatchSnapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

'use strict';

const fs = require('fs');
const mkdirp = require('mkdirp');
const {makeTemplate, makeTests, cleanup} = require('../utils');
const path = require('path');
const rimraf = require('rimraf');
const runJest = require('../runJest');
const skipOnWindows = require('skipOnWindows');

Expand All @@ -20,35 +18,18 @@ const TESTS_DIR = path.resolve(DIR, '__tests__');

skipOnWindows.suite();

const makeTemplate = string => {
return values => {
return string.replace(/\$(\d+)/g, (match, number) => {
return values[number - 1];
});
};
};

const cleanup = () => rimraf.sync(TESTS_DIR);

const makeTests = (tests: {[filename: string]: string}) => {
mkdirp.sync(TESTS_DIR);
Object.keys(tests).forEach(filename => {
fs.writeFileSync(path.resolve(TESTS_DIR, filename), tests[filename]);
});
};

beforeEach(cleanup);
afterAll(cleanup);
beforeEach(() => cleanup(TESTS_DIR));
afterAll(() => cleanup(TESTS_DIR));

test('basic support', () => {
const filename = 'basic-support-test.js';
const template = makeTemplate(
`test('snapshots', () => expect($1).toMatchSnapshot());`,
);

makeTests({[filename]: template(['{apple: "original value"}'])});

{
makeTests(TESTS_DIR, {[filename]: template(['{apple: "original value"}'])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch('1 snapshot written in 1 test file.');
expect(status).toBe(0);
Expand All @@ -60,9 +41,9 @@ test('basic support', () => {
expect(status).toBe(0);
}

makeTests({[filename]: template(['{apple: "updated value"}'])});

{
makeTests(TESTS_DIR, {[filename]: template(['{apple: "updated value"}'])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch('Received value does not match stored snapshot');
expect(status).toBe(1);
Expand All @@ -84,9 +65,9 @@ test.skip('error thrown before snapshot', () => {
});`,
);

makeTests({[filename]: template(['true', '{apple: "original value"}'])});

{
makeTests(TESTS_DIR, {[filename]: template(['true', '{a: "original"}'])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch('1 snapshot written in 1 test file.');
expect(status).toBe(0);
Expand All @@ -97,9 +78,9 @@ test.skip('error thrown before snapshot', () => {
expect(status).toBe(0);
}

makeTests({[filename]: template(['false', '{apple: "original value"}'])});

{
makeTests(TESTS_DIR, {[filename]: template(['false', '{a: "original"}'])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).not.toMatch('1 obsolete snapshot found');
expect(status).toBe(0);
Expand All @@ -115,17 +96,17 @@ test('first snapshot fails, second passes', () => {
});`,
);

makeTests({[filename]: template([`'apple'`, `'banana'`])});

{
makeTests(TESTS_DIR, {[filename]: template([`'apple'`, `'banana'`])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch('2 snapshots written in 1 test file.');
expect(status).toBe(0);
}

makeTests({[filename]: template([`'kiwi'`, `'banana'`])});

{
makeTests(TESTS_DIR, {[filename]: template([`'kiwi'`, `'banana'`])});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch('Received value does not match stored snapshot');
expect(stderr).toMatch('- "apple"\n + "kiwi"');
Expand Down
22 changes: 22 additions & 0 deletions integration_tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

const {spawnSync} = require('child_process');
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const rimraf = require('rimraf');

const run = (cmd, cwd) => {
const args = cmd.split(/\s/).slice(1);
Expand Down Expand Up @@ -46,7 +48,27 @@ const fileExists = filePath => {
}
};

const makeTemplate = string => {
return values => {
return string.replace(/\$(\d+)/g, (match, number) => {
return values[number - 1];
});
};
};

const cleanup = (directory: string) => rimraf.sync(directory);

const makeTests = (directory: string, tests: {[filename: string]: string}) => {
mkdirp.sync(directory);
Object.keys(tests).forEach(filename => {
fs.writeFileSync(path.resolve(directory, filename), tests[filename]);
});
};

module.exports = {
cleanup,
makeTests,
makeTemplate,
fileExists,
linkJestPackage,
run,
Expand Down

0 comments on commit a5de858

Please sign in to comment.