-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
62 lines (54 loc) · 2.93 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {transformAsync} from '@babel/core';
import test from 'ava';
import stripDebugPlugin from './index.js';
async function stripDebug(source) {
const {code} = await transformAsync(source, {
plugins: [stripDebugPlugin],
});
return code;
}
test('strip debugger statement', async t => {
t.is(await stripDebug('function test(){debugger;}'), 'function test() {}');
t.is(await stripDebug('"use strict";debugger;foo()'), '"use strict";\n\nfoo();');
});
test('strip console statement', async t => {
t.is(await stripDebug('function test(){console.log("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('function test(){console.warn("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('function test(){window.console.log("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('function test(){globalThis.console.log("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('var test = () => console.log("foo");'), 'var test = () => void 0;');
t.is(await stripDebug('"use strict";console.log("foo");foo()'), '"use strict";\n\nvoid 0;\nfoo();');
t.is(await stripDebug('if(console){console.log("foo", "bar");}'), 'if (console) {\n void 0;\n}');
t.is(await stripDebug('foo && console.log("foo");'), 'foo && void 0;');
t.is(await stripDebug('if (foo) console.log("foo")\nnextLine();'), 'if (foo) void 0;\nnextLine();');
t.is(await stripDebug('function test(){console.log(...colors);}'), 'function test() {\n void 0;\n}');
});
test('strip alert statement', async t => {
t.is(await stripDebug('function test(){alert("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('function test(){window.alert("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('function test(){globalThis.alert("foo");}'), 'function test() {\n void 0;\n}');
t.is(await stripDebug('var test = () => alert("foo");'), 'var test = () => void 0;');
t.is(await stripDebug('"use strict";alert("foo");foo()'), '"use strict";\n\nvoid 0;\nfoo();');
t.is(await stripDebug('if(alert){alert("foo", "bar");}'), 'if (alert) {\n void 0;\n}');
t.is(await stripDebug('foo && alert("foo");'), 'foo && void 0;');
t.is(await stripDebug('if (foo) alert("foo")\nnextLine();'), 'if (foo) void 0;\nnextLine();');
});
test('never strip away non-debugging code', async t => {
const fixture = `var test = {
getReadSections: function () {
foo.console.log('started');
var readSections = window.localStorage.getItestripDebug('storyReadSections') || '[]';
foo.alert('done');
return JSON.parse(readSections);
}
};`;
t.is(await stripDebug(fixture), fixture);
});
test('shouldn\'t leak memory', async t => {
await t.notThrowsAsync(() =>
stripDebug('var obj = null; try { obj = \'something\'; } catch (e) { console.warn(\'NOPE!\'); }'),
);
});
test('supports async functions', async t => {
t.is(await stripDebug('async function test(){debugger; await foo();}'), 'async function test() {\n await foo();\n}');
});