Skip to content

Commit f58f7fc

Browse files
batiskafffCompuIves
authored andcommitted
Issue codesandbox#1350: Add test.each support (codesandbox#1976)
* issue-1350: Fix test run for jest-lite.test * issue-1350: update initialization test description * codesandbox#1350: add test.each support. Make tests for jest-lite runnable. Small refactoring. * Fix typecheck issue * Update jest-each, add necessary dependency
1 parent 869dd54 commit f58f7fc

File tree

5 files changed

+327
-125
lines changed

5 files changed

+327
-125
lines changed

packages/app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
"codesandbox-import-utils": "2.1.2",
164164
"color": "^0.11.4",
165165
"compare-versions": "^3.1.0",
166+
"console": "^0.7.2",
166167
"console-feed": "^2.8.5",
167168
"css-modules-loader-core": "^1.1.0",
168169
"cssnano": "^3.10.0",
@@ -192,6 +193,7 @@
192193
"instantsearch.css": "^7.1.0",
193194
"is-url": "^1.2.2",
194195
"jest-circus": "^22.1.4",
196+
"jest-each": "^24.8.0",
195197
"jest-snapshot": "^22.1.2",
196198
"jszip": "^3.1.3",
197199
"localforage": "^1.5.5",

packages/app/src/app/components/Preview/DevTools/Tests/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TestDetailsContent from './TestDetails';
1212
import TestSummary from './TestSummary';
1313
import TestOverview from './TestOverview';
1414
import { DevToolProps } from '..';
15+
import { messages } from './../../../../../../src/sandbox/eval/tests/jest-lite';
1516

1617
export type IMessage = {
1718
type: 'message' | 'command' | 'return';
@@ -80,7 +81,7 @@ type SandboxMessage = { type: 'test' | 'done' } & (
8081
| TestEndMessage);
8182

8283
interface InitializedTestsMessage {
83-
event: 'initialize_tests';
84+
event: messages.INITIALIZE;
8485
}
8586

8687
interface TestCountMessage {
@@ -203,7 +204,7 @@ class Tests extends React.Component<DevToolProps, State> {
203204
this.runAllTests();
204205
} else if (data.type === 'test') {
205206
switch (data.event) {
206-
case 'initialize_tests': {
207+
case messages.INITIALIZE: {
207208
this.currentDescribeBlocks = [];
208209
if (this.props.updateStatus) {
209210
this.props.updateStatus('clear');

packages/app/src/sandbox/eval/tests/jest-lite.test.js

Lines changed: 107 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
// import TestRunner from './jest-lite';
2-
const TestRunner = {};
1+
import TestRunner, { messages } from './jest-lite';
32

4-
describe.skip('TestRunner class', () => {
3+
jest.mock('sandbox-hooks/react-error-overlay/utils/mapper', () => {
4+
return {
5+
map: jest.fn(),
6+
};
7+
});
8+
9+
jest.mock('codesandbox-api');
10+
const api = require('codesandbox-api');
11+
12+
api.dispatch = jest.fn();
13+
14+
describe('TestRunner class', () => {
515
it('exports a module', () => {
616
expect(TestRunner).toEqual(expect.any(Function));
717
});
@@ -13,63 +23,65 @@ describe.skip('TestRunner class', () => {
1323
});
1424

1525
describe('initialize', () => {
16-
it('should reset results', () => {
17-
const testRunner = new TestRunner();
18-
expect(testRunner.aggregatedResults.totalTests).toBe(0);
19-
testRunner.addResult({ status: 'pass', name: 'foo' });
20-
expect(testRunner.aggregatedResults.totalTests).toBe(1);
21-
testRunner.initialize();
22-
expect(testRunner.aggregatedResults.totalTests).toBe(0);
26+
let testRunner;
27+
beforeEach(() => {
28+
TestRunner.sendMessage = jest.fn();
29+
testRunner = new TestRunner();
30+
});
31+
32+
it('should be created with 0 test ran', () => {
33+
expect(testRunner.ranTests.size).toBe(0);
34+
});
35+
36+
it('should send message (dispatch) on initilaization', () => {
37+
expect(api.dispatch).toHaveBeenCalledWith({
38+
type: 'test',
39+
event: messages.INITIALIZE,
40+
});
2341
});
2442
});
2543

2644
describe('testGlobals', () => {
27-
it('returns an object', () => {
28-
let {
29-
describe: _describe,
45+
it('returns an object', async () => {
46+
const testRunner = new TestRunner();
47+
48+
window.fetch = jest.fn();
49+
window.localStorage = jest.fn();
50+
51+
await testRunner.initJSDOM();
52+
53+
const {
3054
it: _it,
3155
test: _test,
3256
expect: _expect,
3357
jest: _jest,
34-
} = new TestRunner().testGlobals();
35-
36-
expect(_describe).toEqual(expect.any(Function));
37-
expect(_it).toEqual(expect.any(Function));
38-
expect(_test).toEqual(expect.any(Function));
39-
expect(_expect).toEqual(expect.any(Function));
40-
expect(_jest).toEqual(expect.any(Object));
58+
document: _document,
59+
window: _window,
60+
global: _global,
61+
} = testRunner.testGlobals();
62+
63+
expect(_it).toBeInstanceOf(Function);
64+
expect(_test).toBeInstanceOf(Function);
65+
expect(_expect).toBeInstanceOf(Function);
66+
expect(_jest).toBeInstanceOf(Object);
67+
expect(_document).toBeInstanceOf(Object);
68+
expect(_window).toBeInstanceOf(Object);
69+
expect(_global).toBeInstanceOf(Object);
4170
});
4271

43-
describe('describe', () => {
44-
let testRunner;
45-
let _describe;
46-
let fnSpy;
47-
48-
beforeAll(() => {
49-
testRunner = new TestRunner();
50-
_describe = testRunner.testGlobals().describe;
51-
fnSpy = jest.fn();
52-
});
53-
54-
it('calls the function block', () => {
55-
_describe('foo', fnSpy);
56-
expect(fnSpy).toHaveBeenCalled();
57-
});
58-
});
59-
60-
describe('test', () => {
72+
xdescribe('test', () => {
6173
let testRunner;
6274
let _test;
6375
let fnSpy;
6476

65-
beforeEach(() => {
77+
beforeEach(async () => {
6678
testRunner = new TestRunner();
79+
await testRunner.initJSDOM();
6780
_test = testRunner.testGlobals().test;
6881
fnSpy = jest.fn();
6982
});
7083

7184
it('calls the function block', () => {
72-
_test('foo', fnSpy);
7385
expect(fnSpy).toHaveBeenCalled();
7486
});
7587

@@ -97,50 +109,58 @@ describe.skip('TestRunner class', () => {
97109
});
98110

99111
it('should be initialized with no tests', () => {
100-
expect(testRunner.tests).toEqual([]);
112+
expect(testRunner.ranTests.size).toBe(0);
101113
});
102114
it('should not find any tests when no modules are passed', () => {
103-
testRunner.findTests([]);
115+
testRunner.findTests({});
104116
expect(testRunner.tests).toEqual([]);
105117
});
106118

107-
it('should find tests when modules are passed', () => {
108-
testRunner.findTests([{ path: 'Sum.test.js' }]);
119+
it('should find 1 test when modules are passed', () => {
120+
testRunner.findTests({ 'Sum.test.js': { path: 'Sum.test.js' } });
109121
expect(testRunner.tests).toHaveLength(1);
122+
});
110123

111-
testRunner.findTests([
112-
{ path: 'Sum.test.js' },
113-
{ path: 'Sum.spec.js' },
114-
{ path: '__tests__/Sum.js' },
115-
{ path: 'Sum.js' },
116-
{ path: 'src/Sum.js' },
117-
{ path: 'src/Sum.js' },
118-
]);
124+
it('should find 3 of 6 tests when modules are passed', () => {
125+
testRunner.findTests({
126+
'Sum.test.js': { path: 'Sum.test.js' },
127+
'Sum.spec.js': { path: 'Sum.spec.js' },
128+
'__tests__/Sum.js': { path: '__tests__/Sum.js' },
129+
'Sum.js': { path: 'Sum.js' },
130+
'src/Sum.js': { path: 'src/Sum.js' },
131+
'src/Sum.js1': { path: 'src/Sum.js1' },
132+
});
119133
expect(testRunner.tests).toHaveLength(3);
134+
});
120135

121-
testRunner.findTests([
122-
{ path: 'Sum.test.ts' },
123-
{ path: 'Sum.spec.ts' },
124-
{ path: '__tests__/Sum.ts' },
125-
{ path: 'Sum.ts' },
126-
{ path: 'src/Sum.ts' },
127-
]);
136+
it('should find 3 of 5 tests when modules are passed', () => {
137+
testRunner.findTests({
138+
'Sum.test.ts': { path: 'Sum.test.ts' },
139+
'Sum.spec.ts': { path: 'Sum.spec.ts' },
140+
'__tests__/Sum.ts': { path: '__tests__/Sum.ts' },
141+
'Sum.ts': { path: 'Sum.ts' },
142+
'src/Sum.ts': { path: 'src/Sum.ts' },
143+
});
128144
expect(testRunner.tests).toHaveLength(3);
145+
});
129146

130-
testRunner.findTests([
131-
{ path: 'Sum.test.tsx' },
132-
{ path: 'Sum.spec.tsx' },
133-
{ path: '__tests__/Sum.tsx' },
134-
{ path: 'Sum.tsx' },
135-
{ path: 'src/Sum.tsx' },
136-
]);
147+
it('should find 3 of 5 (typescript) tests when modules are passed', () => {
148+
testRunner.findTests({
149+
'Sum.test.tsx': { path: 'Sum.test.tsx' },
150+
'Sum.spec.tsx': { path: 'Sum.spec.tsx' },
151+
'__tests__/Sum.tsx': { path: '__tests__/Sum.tsx' },
152+
'Sum.tsx': { path: 'Sum.tsx' },
153+
'src/Sum.tsx': { path: 'src/Sum.tsx' },
154+
});
137155
expect(testRunner.tests).toHaveLength(3);
156+
});
138157

139-
testRunner.findTests([
140-
{ path: 'Sum.test.js' },
141-
{ path: 'Sum.test.ts' },
142-
{ path: 'Sum.test.tsx' },
143-
]);
158+
it('should find 3 of 3 tests when modules are passed', () => {
159+
testRunner.findTests({
160+
'Sum.test.js': { path: 'Sum.test.js' },
161+
'Sum.test.ts': { path: 'Sum.test.ts' },
162+
'Sum.test.tsx': { path: 'Sum.test.tsx' },
163+
});
144164
expect(testRunner.tests).toHaveLength(3);
145165
});
146166
});
@@ -153,24 +173,14 @@ describe.skip('TestRunner class', () => {
153173
it('todo');
154174
});
155175

156-
describe('addResult', () => {
176+
// deprecated
177+
xdescribe('addResult', () => {
157178
let testRunner;
158179

159180
beforeEach(() => {
160181
testRunner = new TestRunner();
161182
});
162183

163-
it('should start off with no test results', () => {
164-
expect(testRunner.aggregatedResults).toMatchObject({
165-
failedTestSuites: 0,
166-
passedTestSuites: 0,
167-
totalTestSuites: 0,
168-
failedTests: 0,
169-
passedTests: 0,
170-
totalTests: 0,
171-
});
172-
});
173-
174184
it('should add pass test results', () => {
175185
testRunner.addResult({ status: 'pass', name: 'foo' });
176186
testRunner.addResult({ status: 'pass', name: 'bar' });
@@ -232,7 +242,8 @@ describe.skip('TestRunner class', () => {
232242
});
233243
});
234244

235-
describe('reportResults', () => {
245+
// deprecated
246+
xdescribe('reportResults', () => {
236247
let testRunner;
237248

238249
beforeEach(() => {
@@ -247,8 +258,8 @@ describe.skip('TestRunner class', () => {
247258
testRunner.addResult({ status: 'pass', name: 'foo' });
248259
testRunner.addResult({ status: 'pass', name: 'bar' });
249260

250-
let results = testRunner.reportResults();
251-
let { summaryMessage } = results;
261+
const results = testRunner.reportResults();
262+
const { summaryMessage } = results;
252263

253264
expect(results).not.toEqual(null);
254265
expect(summaryMessage).toMatch(/Test Summary: 😎/);
@@ -262,8 +273,8 @@ describe.skip('TestRunner class', () => {
262273
testRunner.addResult({ status: 'fail', name: 'foo' });
263274
testRunner.addResult({ status: 'fail', name: 'bar' });
264275

265-
let results = testRunner.reportResults();
266-
let { summaryMessage, failedMessages } = results;
276+
const results = testRunner.reportResults();
277+
const { summaryMessage, failedMessages } = results;
267278

268279
expect(results).not.toEqual(null);
269280
expect(summaryMessage).toMatch(/Test Summary: 👻/);
@@ -290,8 +301,8 @@ describe.skip('TestRunner class', () => {
290301
testRunner.addResult({ status: 'pass', name: 'foo' });
291302
testRunner.addResult({ status: 'fail', name: 'bar' });
292303

293-
let results = testRunner.reportResults();
294-
let { summaryMessage } = results;
304+
const results = testRunner.reportResults();
305+
const { summaryMessage } = results;
295306

296307
expect(results).not.toEqual(null);
297308
expect(summaryMessage).toMatch(/Test Summary: 👻/);
@@ -318,8 +329,8 @@ describe.skip('TestRunner class', () => {
318329
testRunner.setCurrentDescribe('bar');
319330
testRunner.addResult({ status: 'fail', name: 'baz' });
320331

321-
let results = testRunner.reportResults();
322-
let { failedMessages } = results;
332+
const results = testRunner.reportResults();
333+
const { failedMessages } = results;
323334

324335
expect(results).not.toEqual(null);
325336
expect(failedMessages[0]).toMatch(/FAIL/);
@@ -331,8 +342,8 @@ describe.skip('TestRunner class', () => {
331342
testRunner.addResult({ status: 'pass', name: 'bar' });
332343
testRunner.addResult({ status: 'fail', name: 'baz' });
333344

334-
let results = testRunner.reportResults();
335-
let { summaryMessage } = results;
345+
const results = testRunner.reportResults();
346+
const { summaryMessage } = results;
336347

337348
expect(results).not.toEqual(null);
338349
expect(summaryMessage).toMatch(/Test Summary: 👻/);
@@ -353,8 +364,8 @@ describe.skip('TestRunner class', () => {
353364
testRunner.addResult({ status: 'pass', name: 'foo' });
354365
testRunner.addResult({ status: 'fail', name: 'bar' });
355366

356-
let results = testRunner.reportResults();
357-
let { summaryMessage } = results;
367+
const results = testRunner.reportResults();
368+
const { summaryMessage } = results;
358369

359370
expect(results).not.toEqual(null);
360371
expect(summaryMessage).toMatch(/Test Summary: 👻/);

0 commit comments

Comments
 (0)