1 | 'use strict';
|
2 |
|
3 | var tap = require('tap');
|
4 | var spawn = require('child_process').spawn;
|
5 | var concat = require('concat-stream');
|
6 | var hasDynamicImport = require('has-dynamic-import');
|
7 | var assign = require('object.assign');
|
8 |
|
9 | tap.test('importing mjs files', function (t) {
|
10 | hasDynamicImport().then(function (hasSupport) {
|
11 | if (hasSupport) {
|
12 | var tc = function (rows) {
|
13 | t.same(rows.toString('utf8'), [
|
14 | 'TAP version 13',
|
15 | '# mjs-a',
|
16 | 'ok 1 test ran',
|
17 | '# mjs-b',
|
18 | 'ok 2 test ran after mjs-a',
|
19 | '# mjs-c',
|
20 | 'ok 3 test ran after mjs-b',
|
21 | '# mjs-d',
|
22 | 'ok 4 test ran after mjs-c',
|
23 | '# mjs-e',
|
24 | 'ok 5 test ran after mjs-d',
|
25 | '# mjs-f',
|
26 | 'ok 6 test ran after mjs-e',
|
27 | '# mjs-g',
|
28 | 'ok 7 test ran after mjs-f',
|
29 | '# mjs-h',
|
30 | 'ok 8 test ran after mjs-g',
|
31 | '',
|
32 | '1..8',
|
33 | '# tests 8',
|
34 | '# pass 8',
|
35 | '',
|
36 | '# ok'
|
37 | ].join('\n') + '\n\n');
|
38 | };
|
39 |
|
40 | var ps = tape('import/mjs-*.mjs');
|
41 | ps.stdout.pipe(concat(tc));
|
42 | ps.stderr.pipe(process.stderr);
|
43 | ps.on('exit', function (code) {
|
44 | t.equal(code, 0);
|
45 | t.end();
|
46 | });
|
47 | } else {
|
48 | t.pass('does not support dynamic import');
|
49 | t.end();
|
50 | }
|
51 | });
|
52 | });
|
53 |
|
54 | tap.test('importing type: "module" files', function (t) {
|
55 | hasDynamicImport().then(function (hasSupport) {
|
56 | if (hasSupport) {
|
57 | var tc = function (rows) {
|
58 | t.same(rows.toString('utf8'), [
|
59 | 'TAP version 13',
|
60 | '# package-type-a',
|
61 | 'ok 1 test ran',
|
62 | '# package-type-b',
|
63 | 'ok 2 test ran after package-type-a',
|
64 | '# package-type-c',
|
65 | 'ok 3 test ran after package-type-b',
|
66 | '',
|
67 | '1..3',
|
68 | '# tests 3',
|
69 | '# pass 3',
|
70 | '',
|
71 | '# ok'
|
72 | ].join('\n') + '\n\n');
|
73 | };
|
74 |
|
75 | var ps = tape('import/package_type/*.js');
|
76 | ps.stdout.pipe(concat(tc));
|
77 | ps.stderr.pipe(process.stderr);
|
78 | ps.on('exit', function (code) {
|
79 | t.equal(code, 0);
|
80 | t.end();
|
81 | });
|
82 | } else {
|
83 | t.pass('does not support dynamic import');
|
84 | t.end();
|
85 | }
|
86 | });
|
87 | });
|
88 |
|
89 | tap.test('errors importing test files', function (t) {
|
90 | hasDynamicImport().then(function (hasSupport) {
|
91 | var createTest = function (options) {
|
92 | var message = options.error + ' in `' + options.mode + '` mode`';
|
93 | var ps = tape(options.files, { env: { NODE_OPTIONS: '--unhandled-rejections=' + options.mode } });
|
94 | ps.stderr.pipe(concat(options.unhandledRejection(message)));
|
95 | ps.on('exit', function (code/* , sig */) {
|
96 | t.equal(code, options.exitCode, message + ' has exit code ' + options.exitCode);
|
97 | });
|
98 | };
|
99 |
|
100 | var warning = function (message) {
|
101 | return function (rows) {
|
102 | t.match(rows.toString('utf8'), 'UnhandledPromiseRejectionWarning', 'should have unhandled rejection warning: ' + message);
|
103 | };
|
104 | };
|
105 |
|
106 | var noWarning = function (message) {
|
107 | return function (rows) {
|
108 | t.notMatch(rows.toString('utf8'), 'UnhandledPromiseRejectionWarning', 'should not have unhandled rejection warning: ' + message);
|
109 | };
|
110 | };
|
111 |
|
112 | if (hasSupport) {
|
113 | var tests = [{
|
114 | files: 'import/syntax-error.mjs import/mjs-a.mjs import/mjs-b.mjs',
|
115 | error: 'syntax errors in first imported esm file',
|
116 | mode: 'warn',
|
117 | exitCode: 0,
|
118 | unhandledRejection: warning
|
119 | }, {
|
120 | files: 'import/throws.mjs import/mjs-a.mjs import/mjs-b.mjs',
|
121 | error: 'thrown errors in first imported esm file',
|
122 | mode: 'warn',
|
123 | exitCode: 0,
|
124 | unhandledRejection: warning
|
125 | }, {
|
126 | files: 'import/mjs-a.mjs import/syntax-error.mjs',
|
127 | error: 'syntax error in esm file',
|
128 | mode: 'warn',
|
129 | exitCode: 1,
|
130 | unhandledRejection: warning
|
131 | }, {
|
132 | files: 'import/syntax-error.mjs',
|
133 | error: 'syntax error in esm file',
|
134 | mode: 'strict',
|
135 | exitCode: 1,
|
136 | unhandledRejection: noWarning
|
137 | }, {
|
138 | files: 'import/throws.mjs',
|
139 | error: 'thrown error in esm file',
|
140 | mode: 'strict',
|
141 | exitCode: 1,
|
142 | unhandledRejection: noWarning
|
143 | }, {
|
144 | files: 'import/syntax-error.cjs',
|
145 | error: 'syntax error in cjs file',
|
146 | mode: 'warn',
|
147 | exitCode: 1,
|
148 | unhandledRejection: noWarning
|
149 | }, {
|
150 | files: 'import/throws.cjs',
|
151 | error: 'thrown error in cjs file',
|
152 | mode: 'warn',
|
153 | exitCode: 1,
|
154 | unhandledRejection: noWarning
|
155 | }, {
|
156 | files: 'import/syntax-error.cjs',
|
157 | error: 'syntax error in cjs file',
|
158 | mode: 'strict',
|
159 | exitCode: 1,
|
160 | unhandledRejection: noWarning
|
161 | }, {
|
162 | files: 'import/throws.cjs',
|
163 | error: 'thrown error in cjs file',
|
164 | mode: 'strict',
|
165 | exitCode: 1,
|
166 | unhandledRejection: noWarning
|
167 | }, {
|
168 | files: 'import/mjs-a.mjs import/syntax-error.cjs',
|
169 | error: 'syntax error in cjs file in loading promise',
|
170 | mode: 'warn',
|
171 | exitCode: 1,
|
172 | unhandledRejection: warning
|
173 | }, {
|
174 | files: 'import/mjs-a.mjs import/syntax-error.cjs',
|
175 | error: 'syntax error in cjs file in loading promise',
|
176 | mode: 'strict',
|
177 | exitCode: 1,
|
178 | unhandledRejection: noWarning
|
179 | }];
|
180 |
|
181 | t.plan(tests.length * 2);
|
182 |
|
183 | tests.map(createTest);
|
184 | } else {
|
185 | t.pass('does not support dynamic import');
|
186 | t.end();
|
187 | }
|
188 | });
|
189 | });
|
190 |
|
191 | function tape(args, options) {
|
192 | var bin = __dirname + '/../bin/tape';
|
193 |
|
194 | return spawn('node', [bin].concat(args.split(' ')), assign({ cwd: __dirname }, options));
|
195 | }
|