main
js 80 lines 2.18 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @emails react-core
8 */
9
10 'use strict';
11
12 const spawnSync = require('child_process').spawnSync;
13
14 describe('ReactClassEquivalence', () => {
15 it('tests the same thing for es6 classes and CoffeeScript', () => {
16 const result1 = runJest('ReactCoffeeScriptClass-test.coffee');
17 const result2 = runJest('ReactES6Class-test.js');
18 compareResults(result1, result2);
19 });
20
21 it('tests the same thing for es6 classes and TypeScript', () => {
22 const result1 = runJest('ReactTypeScriptClass-test.ts');
23 const result2 = runJest('ReactES6Class-test.js');
24 compareResults(result1, result2);
25 });
26 });
27
28 function runJest(testFile) {
29 const cwd = process.cwd();
30 const extension = process.platform === 'win32' ? '.cmd' : '';
31 const command = process.env.npm_lifecycle_event;
32 const defaultReporter = '--reporters=default';
33 const equivalenceReporter =
34 '--reporters=<rootDir>/scripts/jest/spec-equivalence-reporter/equivalenceReporter.js';
35 if (!command.startsWith('test')) {
36 throw new Error(
37 'Expected this test to run as a result of one of test commands.',
38 );
39 }
40 const result = spawnSync(
41 'yarn' + extension,
42 [command, testFile, defaultReporter, equivalenceReporter],
43 {
44 cwd,
45 env: Object.assign({}, process.env, {
46 REACT_CLASS_EQUIVALENCE_TEST: 'true',
47 }),
48 },
49 );
50
51 if (result.error) {
52 throw result.error;
53 }
54
55 if (result.status !== 0) {
56 throw new Error(
57 'jest process exited with: ' +
58 result.status +
59 '\n' +
60 'stdout: ' +
61 result.stdout.toString() +
62 'stderr: ' +
63 result.stderr.toString(),
64 );
65 }
66
67 return result.stdout.toString() + result.stderr.toString();
68 }
69
70 function compareResults(a, b) {
71 const regexp = /EQUIVALENCE.*$/gm;
72 const aSpecs = (a.match(regexp) || []).sort().join('\n');
73 const bSpecs = (b.match(regexp) || []).sort().join('\n');
74
75 if (aSpecs.length === 0 && bSpecs.length === 0) {
76 throw new Error('No spec results found in the output');
77 }
78
79 expect(aSpecs).toEqual(bSpecs);
80 }