Skip to content

Commit 6186e84

Browse files
committed
feat: hoisting + tests + many other things
1 parent 2dba4fc commit 6186e84

31 files changed

Lines changed: 519 additions & 616 deletions

.npmignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# TypeScript source code
2-
src
3-
41
# Tests
5-
tests
2+
e2e
3+
test
64

75
# Developement scripts
86
scripts

e2e/__cases__/hoisting/Hello.spec.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

e2e/__cases__/hoisting/Hello.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hello from './hello';
2+
3+
jest.mock('./hello');
4+
5+
describe('hello', () => {
6+
const original = require.requireActual('./hello').default;
7+
it('should have been mocked', () => {
8+
const msg = hello();
9+
expect(hello).not.toBe(original);
10+
expect(msg).toBeUndefined();
11+
expect(hello).toHaveProperty('mock');
12+
});
13+
});

e2e/__cases__/hoisting/hello.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function() {
2+
return 'hi!';
3+
}

e2e/__helpers__/test-case.ts

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,37 @@ import { join } from 'path';
44
import * as Paths from '../../scripts/paths';
55
import * as fs from 'fs-extra';
66

7-
const jestDescribe = describe;
8-
const jestIt = it;
9-
const jestExpect = expect;
10-
117
const TEMPLATE_EXCLUDED_ITEMS = ['node_modules', 'package-lock.json'];
128

139
type RunWithTemplatesIterator = (
1410
runtTest: () => TestRunResult,
15-
templateName: string,
11+
context: RunWithTemplateIteratorContext,
1612
) => void;
17-
interface RunWithTemplatesOptions {
18-
iterator?: RunWithTemplatesIterator;
19-
logUnlessStatus?: number;
20-
}
21-
interface WithTemplatesIteratorOptions {
22-
describe?: string | false;
23-
it?: string;
24-
expect?: RunWithTemplatesIterator;
13+
14+
interface RunWithTemplateIteratorContext {
15+
templateName: string;
16+
describeLabel: string;
17+
itLabel: string;
18+
testLabel: string;
2519
}
2620

27-
export function withTemplatesIterator({
28-
describe = 'with template "__TEMPLATE_NAME__"',
29-
it = 'should run as expected',
30-
expect = runTest => {
31-
jestExpect(runTest()).toMatchSnapshot();
32-
},
33-
}: WithTemplatesIteratorOptions = {}): RunWithTemplatesIterator {
34-
return (runTest, name) => {
35-
const interpolate = (msg: string) => msg.replace('__TEMPLATE_NAME__', name);
36-
if (describe) {
37-
jestDescribe(interpolate(describe), () => {
38-
jestIt(interpolate(it), () => expect(runTest, name));
39-
});
40-
} else {
41-
jestIt(interpolate(it), () => expect(runTest, name));
21+
function createIteratorContext(
22+
templateName: string,
23+
expectedStatus?: number,
24+
): RunWithTemplateIteratorContext {
25+
const actionForExpectedStatus = (status?: number): string => {
26+
if (status == null) {
27+
return 'run';
4228
}
29+
return status === 0 ? 'pass' : 'fail';
30+
};
31+
return {
32+
templateName,
33+
describeLabel: `with template "${templateName}"`,
34+
itLabel: `should ${actionForExpectedStatus(expectedStatus)}`,
35+
testLabel: `should ${actionForExpectedStatus(
36+
expectedStatus,
37+
)} using template "${templateName}"`,
4338
};
4439
}
4540

@@ -94,13 +89,15 @@ class TestCaseRunDescriptor {
9489

9590
runWithTemplates<T extends string>(
9691
templates: T[],
97-
{ iterator, logUnlessStatus }: RunWithTemplatesOptions = {},
92+
expectedStatus?: number,
93+
iterator?: RunWithTemplatesIterator,
9894
): TestRunResultsMap<T> {
9995
if (templates.length < 1) {
10096
throw new RangeError(
10197
`There must be at least one template to run the test case with.`,
10298
);
10399
}
100+
104101
if (!templates.every((t, i) => templates.indexOf(t, i + 1) === -1)) {
105102
throw new Error(
106103
`Each template must be unique. Given ${templates.join(', ')}`,
@@ -113,12 +110,12 @@ class TestCaseRunDescriptor {
113110
template,
114111
});
115112
const runTest = () => {
116-
const out = desc.run(logUnlessStatus);
113+
const out = desc.run(expectedStatus);
117114
map[template] = { ...out };
118115
return out;
119116
};
120117
if (iterator) {
121-
iterator(runTest, template);
118+
iterator(runTest, createIteratorContext(template, expectedStatus));
122119
} else {
123120
runTest();
124121
}
@@ -151,7 +148,7 @@ export type TestRunResultsMap<T extends string = string> = {
151148
[key in T]: TestRunResult
152149
};
153150

154-
export default function configureTestCase(
151+
export function configureTestCase(
155152
name: string,
156153
options: RunTestOptions = {},
157154
): TestCaseRunDescriptor {
@@ -246,17 +243,19 @@ function stripAnsiColors(stringToStrip: string): string {
246243

247244
function prepareTest(name: string, template: string): string {
248245
const sourceDir = join(Paths.e2eSourceDir, name);
249-
// working directory is in the temp directory, different for each tempalte name
246+
// working directory is in the temp directory, different for each template name
250247
const caseDir = join(Paths.e2eWorkDir, template, name);
251248
const templateDir = join(Paths.e2eWorkTemplatesDir, template);
252249

253-
// ensure directory exists
250+
// recreate the directory
251+
fs.removeSync(caseDir);
254252
fs.mkdirpSync(caseDir);
255253

256-
// link the node_modules dir if the template has
257254
const tmplModulesDir = join(templateDir, 'node_modules');
258255
const caseModulesDir = join(caseDir, 'node_modules');
259-
if (!fs.existsSync(caseModulesDir) && fs.existsSync(tmplModulesDir)) {
256+
257+
// link the node_modules dir if the template has one
258+
if (fs.existsSync(tmplModulesDir)) {
260259
fs.symlinkSync(tmplModulesDir, caseModulesDir);
261260
}
262261

e2e/__templates__/with-babel-7/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = {
55
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
66
moduleFileExtensions: ['ts', 'js'],
77
testEnvironment: 'node',
8-
globals: { 'ts-jest': { tsConfig: {} } },
8+
globals: { 'ts-jest': { tsConfig: {}, useBabelJest: true } },
99
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Hoisting test with template "default" should pass 1`] = `
4+
jest exit code: 0
5+
===[ STDOUT ]===================================================================
6+
7+
===[ STDERR ]===================================================================
8+
PASS ./hello.spec.ts
9+
hello
10+
√ should have been mocked
11+
12+
Test Suites: 1 passed, 1 total
13+
Tests: 1 passed, 1 total
14+
Snapshots: 0 total
15+
Time: XXs
16+
Ran all test suites.
17+
================================================================================
18+
`;
19+
20+
exports[`Hoisting test with template "with-babel-6" should pass 1`] = `
21+
jest exit code: 0
22+
===[ STDOUT ]===================================================================
23+
24+
===[ STDERR ]===================================================================
25+
PASS ./hello.spec.ts
26+
hello
27+
√ should have been mocked
28+
29+
Test Suites: 1 passed, 1 total
30+
Tests: 1 passed, 1 total
31+
Snapshots: 0 total
32+
Time: XXs
33+
Ran all test suites.
34+
================================================================================
35+
`;
36+
37+
exports[`Hoisting test with template "with-babel-7" should pass 1`] = `
38+
jest exit code: 0
39+
===[ STDOUT ]===================================================================
40+
41+
===[ STDERR ]===================================================================
42+
PASS ./hello.spec.ts
43+
hello
44+
√ should have been mocked
45+
46+
Test Suites: 1 passed, 1 total
47+
Tests: 1 passed, 1 total
48+
Snapshots: 0 total
49+
Time: XXs
50+
Ran all test suites.
51+
================================================================================
52+
`;
53+
54+
exports[`Hoisting test with template "with-jest-22" should pass 1`] = `
55+
jest exit code: 0
56+
===[ STDOUT ]===================================================================
57+
58+
===[ STDERR ]===================================================================
59+
PASS ./hello.spec.ts
60+
hello
61+
√ should have been mocked
62+
63+
Test Suites: 1 passed, 1 total
64+
Tests: 1 passed, 1 total
65+
Snapshots: 0 total
66+
Time: XXs
67+
Ran all test suites.
68+
================================================================================
69+
`;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)