Skip to content

Commit b41f402

Browse files
Jack ZhaoTimer
Jack Zhao
authored andcommitted
Convert test suite to Jest (facebook#4550)
* convert mocha tests to jest * jest 23 * add jest configs * use material css * fix windows * forceExit jest test * force exit eject * test * test * retrigger test * remove appveyor comment * try to remove pretendToBeVisual option * use jsdom env * test environment * no cache * test no close * bring back raf * test revert all broken changes * add back jsdom * remove jsdom * node test environment * use latest change * runInBand * runInBand * comment test run * try different jest option * standardize jest test options * increase heap size * remove heap size config * support scoped packages for cra --scripts-version option * upgrade jest version * fix windows * fix windows again * jest 23.4.1 * babel-jest * babel-jest * split out kitchhensink * travis node 6 * travis node 6 config * node 6 travis eject * cache yarn * only cache yarn * remove unrelated changes * typo
1 parent 16e0254 commit b41f402

File tree

9 files changed

+63
-67
lines changed

9 files changed

+63
-67
lines changed

fixtures/kitchensink/.template.dependencies.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
{
22
"dependencies": {
3-
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.46",
4-
"@babel/polyfill": "7.0.0-beta.46",
5-
"@babel/register": "7.0.0-beta.46",
6-
"bootstrap": "4.1.0",
7-
"chai": "3.5.0",
8-
"jsdom": "9.8.3",
9-
"mocha": "3.2.0",
3+
"bootstrap": "4.1.1",
4+
"jest": "23.6.0",
105
"node-sass": "4.8.3",
116
"normalize.css": "7.0.0",
127
"prop-types": "15.5.6",

fixtures/kitchensink/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,3 @@ An usual flow for the test itself is something similar to:
5151

5252
- since `initDOM` returns a `Document` element, the previous `id` attribute is used to target the feature's DOM and `expect` accordingly
5353

54-
These tests are run by **mocha** (why not **jest**? See [this issue](https://github.com/facebook/jest/issues/2288)) and the environments used are both `development` and `production`.

fixtures/kitchensink/integration/env.test.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { expect } from 'chai';
98
import initDOM from './initDOM';
109

1110
describe('Integration', () => {
@@ -15,23 +14,23 @@ describe('Integration', () => {
1514

1615
expect(
1716
doc.getElementById('feature-file-env-original-1').textContent
18-
).to.equal('from-original-env-1');
17+
).toBe('from-original-env-1');
1918
expect(
2019
doc.getElementById('feature-file-env-original-2').textContent
21-
).to.equal('override-from-original-local-env-2');
20+
).toBe('override-from-original-local-env-2');
2221

2322
if (process.env.NODE_ENV === 'production') {
24-
expect(doc.getElementById('feature-file-env').textContent).to.equal(
23+
expect(doc.getElementById('feature-file-env').textContent).toBe(
2524
'production'
2625
);
27-
expect(doc.getElementById('feature-file-env-x').textContent).to.equal(
26+
expect(doc.getElementById('feature-file-env-x').textContent).toBe(
2827
'x-from-production-env'
2928
);
3029
} else {
31-
expect(doc.getElementById('feature-file-env').textContent).to.equal(
30+
expect(doc.getElementById('feature-file-env').textContent).toBe(
3231
'development'
3332
);
34-
expect(doc.getElementById('feature-file-env-x').textContent).to.equal(
33+
expect(doc.getElementById('feature-file-env-x').textContent).toBe(
3534
'x-from-development-env'
3635
);
3736
}
@@ -41,9 +40,7 @@ describe('Integration', () => {
4140
it('NODE_PATH', async () => {
4241
const doc = await initDOM('node-path');
4342

44-
expect(
45-
doc.getElementById('feature-node-path').childElementCount
46-
).to.equal(4);
43+
expect(doc.getElementById('feature-node-path').childElementCount).toBe(4);
4744
doc.defaultView.close();
4845
});
4946

@@ -54,12 +51,12 @@ describe('Integration', () => {
5451
process.env.NODE_ENV === 'development'
5552
? ''
5653
: 'http://www.example.org/spa';
57-
expect(doc.getElementById('feature-public-url').textContent).to.equal(
54+
expect(doc.getElementById('feature-public-url').textContent).toBe(
5855
`${prefix}.`
5956
);
6057
expect(
6158
doc.querySelector('head link[rel="shortcut icon"]').getAttribute('href')
62-
).to.equal(`${prefix}/favicon.ico`);
59+
).toBe(`${prefix}/favicon.ico`);
6360
doc.defaultView.close();
6461
});
6562

@@ -68,25 +65,25 @@ describe('Integration', () => {
6865

6966
expect(
7067
doc.getElementById('feature-shell-env-variables').textContent
71-
).to.equal('fromtheshell.');
68+
).toBe('fromtheshell.');
7269
doc.defaultView.close();
7370
});
7471

7572
it('expand .env variables', async () => {
7673
const doc = await initDOM('expand-env-variables');
7774

78-
expect(doc.getElementById('feature-expand-env-1').textContent).to.equal(
75+
expect(doc.getElementById('feature-expand-env-1').textContent).toBe(
7976
'basic'
8077
);
81-
expect(doc.getElementById('feature-expand-env-2').textContent).to.equal(
78+
expect(doc.getElementById('feature-expand-env-2').textContent).toBe(
8279
'basic'
8380
);
84-
expect(doc.getElementById('feature-expand-env-3').textContent).to.equal(
81+
expect(doc.getElementById('feature-expand-env-3').textContent).toBe(
8582
'basic'
8683
);
8784
expect(
8885
doc.getElementById('feature-expand-env-existing').textContent
89-
).to.equal('fromtheshell');
86+
).toBe('fromtheshell');
9087
doc.defaultView.close();
9188
});
9289
});

fixtures/kitchensink/integration/initDOM.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
const fs = require('fs');
99
const http = require('http');
10-
const jsdom = require('jsdom');
10+
const jsdom = require('jsdom/lib/old-api.js');
1111
const path = require('path');
12-
const { expect } = require('chai');
1312

1413
let getMarkup;
1514
export let resourceLoader;
@@ -50,7 +49,7 @@ if (process.env.E2E_FILE) {
5049
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
5150
expect(
5251
new Error("This isn't the error you are looking for.")
53-
).to.be.undefined();
52+
).toBeUndefined();
5453
});
5554
}
5655

fixtures/kitchensink/integration/syntax.test.js

+21-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { expect } from 'chai';
98
import initDOM from './initDOM';
109

1110
describe('Integration', () => {
@@ -15,25 +14,25 @@ describe('Integration', () => {
1514

1615
expect(
1716
doc.getElementById('feature-array-destructuring').childElementCount
18-
).to.equal(4);
17+
).toBe(4);
1918
doc.defaultView.close();
2019
});
2120

2221
it('array spread', async () => {
2322
const doc = await initDOM('array-spread');
2423

25-
expect(
26-
doc.getElementById('feature-array-spread').childElementCount
27-
).to.equal(4);
24+
expect(doc.getElementById('feature-array-spread').childElementCount).toBe(
25+
4
26+
);
2827
doc.defaultView.close();
2928
});
3029

3130
it('async/await', async () => {
3231
const doc = await initDOM('async-await');
3332

34-
expect(
35-
doc.getElementById('feature-async-await').childElementCount
36-
).to.equal(4);
33+
expect(doc.getElementById('feature-async-await').childElementCount).toBe(
34+
4
35+
);
3736
doc.defaultView.close();
3837
});
3938

@@ -42,7 +41,7 @@ describe('Integration', () => {
4241

4342
expect(
4443
doc.getElementById('feature-class-properties').childElementCount
45-
).to.equal(4);
44+
).toBe(4);
4645
doc.defaultView.close();
4746
});
4847

@@ -51,7 +50,7 @@ describe('Integration', () => {
5150

5251
expect(
5352
doc.getElementById('feature-computed-properties').childElementCount
54-
).to.equal(4);
53+
).toBe(4);
5554
doc.defaultView.close();
5655
});
5756

@@ -60,7 +59,7 @@ describe('Integration', () => {
6059

6160
expect(
6261
doc.getElementById('feature-custom-interpolation').childElementCount
63-
).to.equal(4);
62+
).toBe(4);
6463
doc.defaultView.close();
6564
});
6665

@@ -69,7 +68,7 @@ describe('Integration', () => {
6968

7069
expect(
7170
doc.getElementById('feature-default-parameters').childElementCount
72-
).to.equal(4);
71+
).toBe(4);
7372
doc.defaultView.close();
7473
});
7574

@@ -78,16 +77,16 @@ describe('Integration', () => {
7877

7978
expect(
8079
doc.getElementById('feature-destructuring-and-await').childElementCount
81-
).to.equal(4);
80+
).toBe(4);
8281
doc.defaultView.close();
8382
});
8483

8584
it('generators', async () => {
8685
const doc = await initDOM('generators');
8786

88-
expect(
89-
doc.getElementById('feature-generators').childElementCount
90-
).to.equal(4);
87+
expect(doc.getElementById('feature-generators').childElementCount).toBe(
88+
4
89+
);
9190
doc.defaultView.close();
9291
});
9392

@@ -96,7 +95,7 @@ describe('Integration', () => {
9695

9796
expect(
9897
doc.getElementById('feature-object-destructuring').childElementCount
99-
).to.equal(4);
98+
).toBe(4);
10099
doc.defaultView.close();
101100
});
102101

@@ -105,16 +104,14 @@ describe('Integration', () => {
105104

106105
expect(
107106
doc.getElementById('feature-object-spread').childElementCount
108-
).to.equal(4);
107+
).toBe(4);
109108
doc.defaultView.close();
110109
});
111110

112111
it('promises', async () => {
113112
const doc = await initDOM('promises');
114113

115-
expect(doc.getElementById('feature-promises').childElementCount).to.equal(
116-
4
117-
);
114+
expect(doc.getElementById('feature-promises').childElementCount).toBe(4);
118115
doc.defaultView.close();
119116
});
120117

@@ -123,7 +120,7 @@ describe('Integration', () => {
123120

124121
expect(
125122
doc.getElementById('feature-rest-and-default').childElementCount
126-
).to.equal(4);
123+
).toBe(4);
127124
doc.defaultView.close();
128125
});
129126

@@ -132,7 +129,7 @@ describe('Integration', () => {
132129

133130
expect(
134131
doc.getElementById('feature-rest-parameters').childElementCount
135-
).to.equal(4);
132+
).toBe(4);
136133
doc.defaultView.close();
137134
});
138135

@@ -141,7 +138,7 @@ describe('Integration', () => {
141138

142139
expect(
143140
doc.getElementById('feature-template-interpolation').childElementCount
144-
).to.equal(4);
141+
).toBe(4);
145142
doc.defaultView.close();
146143
});
147144
});

fixtures/kitchensink/integration/webpack.test.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { expect } from 'chai';
98
import initDOM, { resourceLoader } from './initDOM';
109
import url from 'url';
1110

@@ -20,14 +19,14 @@ const matchCSS = (doc, regexes) => {
2019
}
2120
resourceLoader({ url: url.parse(href) }, (_, textContent) => {
2221
for (const regex of regexes) {
23-
expect(textContent).to.match(regex);
22+
expect(textContent).toMatch(regex);
2423
}
2524
});
2625
} else {
2726
for (let i = 0; i < regexes.length; ++i) {
2827
expect(
2928
doc.getElementsByTagName('style')[i].textContent.replace(/\s/g, '')
30-
).to.match(regexes[i]);
29+
).toMatch(regexes[i]);
3130
}
3231
}
3332
};
@@ -87,7 +86,7 @@ describe('Integration', () => {
8786
const children = doc.getElementById('graphql-inclusion').children;
8887

8988
// .graphql
90-
expect(children[0].textContent.replace(/\s/g, '')).to.equal(
89+
expect(children[0].textContent.replace(/\s/g, '')).toBe(
9190
'{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"test"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"test"},"value":{"kind":"StringValue","value":"test","block":false}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"test"},"arguments":[],"directives":[]}]}}]}}],"loc":{"start":0,"end":40,"source":{"body":"{\\ntest(test:\\"test\\"){\\ntest\\n}\\n}\\n","name":"GraphQLrequest","locationOffset":{"line":1,"column":1}}}}'
9291
);
9392
doc.defaultView.close();
@@ -96,7 +95,7 @@ describe('Integration', () => {
9695
it('image inclusion', async () => {
9796
const doc = await initDOM('image-inclusion');
9897

99-
expect(doc.getElementById('feature-image-inclusion').src).to.match(
98+
expect(doc.getElementById('feature-image-inclusion').src).toMatch(
10099
/^data:image\/jpeg;base64.+==$/
101100
);
102101
doc.defaultView.close();
@@ -105,7 +104,7 @@ describe('Integration', () => {
105104
it('no ext inclusion', async () => {
106105
const doc = await initDOM('no-ext-inclusion');
107106

108-
expect(doc.getElementById('feature-no-ext-inclusion').href).to.match(
107+
expect(doc.getElementById('feature-no-ext-inclusion').href).toMatch(
109108
/\/static\/media\/aFileWithoutExt\.[a-f0-9]{8}\.bin$/
110109
);
111110
doc.defaultView.close();
@@ -114,7 +113,7 @@ describe('Integration', () => {
114113
it('json inclusion', async () => {
115114
const doc = await initDOM('json-inclusion');
116115

117-
expect(doc.getElementById('feature-json-inclusion').textContent).to.equal(
116+
expect(doc.getElementById('feature-json-inclusion').textContent).toBe(
118117
'This is an abstract.'
119118
);
120119
doc.defaultView.close();
@@ -123,15 +122,15 @@ describe('Integration', () => {
123122
it('linked modules', async () => {
124123
const doc = await initDOM('linked-modules');
125124

126-
expect(doc.getElementById('feature-linked-modules').textContent).to.equal(
125+
expect(doc.getElementById('feature-linked-modules').textContent).toBe(
127126
'2.0.0'
128127
);
129128
doc.defaultView.close();
130129
});
131130

132131
it('svg inclusion', async () => {
133132
const doc = await initDOM('svg-inclusion');
134-
expect(doc.getElementById('feature-svg-inclusion').src).to.match(
133+
expect(doc.getElementById('feature-svg-inclusion').src).toMatch(
135134
/\/static\/media\/logo\..+\.svg$/
136135
);
137136
doc.defaultView.close();
@@ -140,9 +139,7 @@ describe('Integration', () => {
140139
it('svg component', async () => {
141140
const doc = await initDOM('svg-component');
142141

143-
expect(doc.getElementById('feature-svg-component').textContent).to.equal(
144-
''
145-
);
142+
expect(doc.getElementById('feature-svg-component').textContent).toBe('');
146143
doc.defaultView.close();
147144
});
148145

@@ -155,7 +152,7 @@ describe('Integration', () => {
155152
it('unknown ext inclusion', async () => {
156153
const doc = await initDOM('unknown-ext-inclusion');
157154

158-
expect(doc.getElementById('feature-unknown-ext-inclusion').href).to.match(
155+
expect(doc.getElementById('feature-unknown-ext-inclusion').href).toMatch(
159156
/\/static\/media\/aFileWithExt\.[a-f0-9]{8}\.unknown$/
160157
);
161158
doc.defaultView.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testMatch: ['**/integration/*.test.js'],
4+
};

0 commit comments

Comments
 (0)