UNPKG

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