|
| 1 | +/** @jest-environment node */ |
| 2 | + |
| 3 | + |
| 4 | +const { spawn, exec } = require('child_process'); |
| 5 | + |
| 6 | +const NODE_14_TEST_CASES = [ |
| 7 | + ['node-14'], |
| 8 | + ["node-14"], |
| 9 | + ["node-14-jsdom"], |
| 10 | + ["node-14-jest-env-node"], |
| 11 | + ["node-14-jest-env-node-jsdom"], |
| 12 | + ["node-14-jest-env-jsdom"] |
| 13 | +] |
| 14 | + |
| 15 | +const NODE_15_TEST_CASES = [ |
| 16 | + ['node-15'], |
| 17 | + ["node-15"], |
| 18 | + ["node-15-jsdom"], |
| 19 | + ["node-15-jest-env-node"], |
| 20 | + ["node-15-jest-env-node-jsdom"], |
| 21 | + ["node-15-jest-env-jsdom"] |
| 22 | +]; |
| 23 | + |
| 24 | +describe.each([['Node <15', NODE_14_TEST_CASES], ['Node 15', NODE_15_TEST_CASES]])('$s', (label, cases) => { |
| 25 | + if (process.env.DEBUG) { |
| 26 | + console.log('Running', label); |
| 27 | + } |
| 28 | + |
| 29 | +it.each(cases)('%s', (command, done) => { |
| 30 | + const test = spawn('yarn', [command], {detached: true}); |
| 31 | + let finished = false; |
| 32 | + let timeout; |
| 33 | + |
| 34 | + function debug(...args) { |
| 35 | + if (!finished && process.env.DEBUG) { |
| 36 | + console.log(command, ...args); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + debug(`testing ${command}...`); |
| 41 | + |
| 42 | + function processResult(reason) { |
| 43 | + if (!finished) { |
| 44 | + finished = true; |
| 45 | + clearTimeout(timeout); |
| 46 | + try { |
| 47 | + expect(command).toPass(reason); |
| 48 | + } finally { |
| 49 | + done(); |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + test.stderr.on('data', (data) => { |
| 56 | + debug('err', data.toString()); |
| 57 | + if (data.toString().indexOf('STARTED') >= 0) { |
| 58 | + if (!timeout) { |
| 59 | + debug('schduling stderr timeout'); |
| 60 | + timeout = setTimeout(() => { |
| 61 | + debug('timed out in stderr, killing'); |
| 62 | + test.kill('SIGKILL'); |
| 63 | + processResult('TIMEOUT'); |
| 64 | + }, 10000); |
| 65 | + } else { |
| 66 | + debug('timeout already set'); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + test.stdout.on('data', (data) => { |
| 71 | + debug('out', data.toString()); |
| 72 | + if (data.toString().indexOf('STARTED') >= 0) { |
| 73 | + if (!timeout) { |
| 74 | + debug('schduling stdout timeout'); |
| 75 | + timeout = setTimeout(() => { |
| 76 | + debug('timed out in out, killing'); |
| 77 | + process.kill(-test.pid); |
| 78 | + processResult('TIMEOUT'); |
| 79 | + }, 10000); |
| 80 | + } else { |
| 81 | + debug('timeout already set'); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + test.on('close', (code) => { |
| 86 | + debug('closed', code); |
| 87 | + |
| 88 | + if (code !== 0) { |
| 89 | + processResult('FAILED'); |
| 90 | + } else { |
| 91 | + processResult('SUCCESS'); |
| 92 | + } |
| 93 | + |
| 94 | + }); |
| 95 | + |
| 96 | + test.on('error', (err) => { |
| 97 | + debug('error', err); |
| 98 | + processResult('ERROR'); |
| 99 | + }); |
| 100 | + |
| 101 | + test.on('SIGKILL', () => { |
| 102 | + debug('killed'); |
| 103 | + processResult('KILLED'); |
| 104 | + }); |
| 105 | +}); |
| 106 | +}); |
| 107 | + |
| 108 | +expect.extend({ |
| 109 | + toPass(command, reason) { |
| 110 | + const pass = reason === 'SUCCESS'; |
| 111 | + function getCommand() { |
| 112 | + return `(yarn ${command})` |
| 113 | + } |
| 114 | + if (pass) { |
| 115 | + return { |
| 116 | + message: () => |
| 117 | + `expected ${command} not to pass`, |
| 118 | + pass: true, |
| 119 | + }; |
| 120 | + } else { |
| 121 | + return { |
| 122 | + message: () => { |
| 123 | + switch(reason) { |
| 124 | + case 'FAILED': |
| 125 | + return `expected ${command} to pass but it failed ${getCommand()}`; |
| 126 | + case 'TIMEOUT': |
| 127 | + return `expected ${command} to pass but it hung ${getCommand()}`; |
| 128 | + case 'ERROR': |
| 129 | + return `expected ${command} to pass but it errored ${getCommand()}`; |
| 130 | + default: |
| 131 | + return `expected ${command} to pass but it failed for an unknown reason ${reason}`; |
| 132 | + } |
| 133 | + }, |
| 134 | + pass: false, |
| 135 | + }; |
| 136 | + } |
| 137 | + }, |
| 138 | +}); |
0 commit comments