-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
babel-spec.js
83 lines (70 loc) · 2.84 KB
/
babel-spec.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Users may have this environment variable set. Currently, it causes babel to
// log to stderr, which causes errors on Windows.
// See https://github.com/atom/electron/issues/2033
process.env.DEBUG = '*';
const path = require('path');
const temp = require('temp').track();
const CompileCache = require('../src/compile-cache');
describe('Babel transpiler support', function() {
let originalCacheDir = null;
beforeEach(function() {
originalCacheDir = CompileCache.getCacheDirectory();
CompileCache.setCacheDirectory(temp.mkdirSync('compile-cache'));
// TODO: rework to avoid using IIFE https://developer.mozilla.org/en-US/docs/Glossary/IIFE
return (() => {
const result = [];
for (let cacheKey of Object.keys(require.cache)) {
if (cacheKey.startsWith(path.join(__dirname, 'fixtures', 'babel'))) {
result.push(delete require.cache[cacheKey]);
} else {
result.push(undefined);
}
}
return result;
})();
});
afterEach(function() {
CompileCache.setCacheDirectory(originalCacheDir);
try {
return temp.cleanupSync();
} catch (error) {}
});
describe('when a .js file starts with /** @babel */;', () =>
it('transpiles it using babel', function() {
const transpiled = require('./fixtures/babel/babel-comment.js');
expect(transpiled(3)).toBe(4);
}));
describe("when a .js file starts with 'use babel';", () =>
it('transpiles it using babel', function() {
const transpiled = require('./fixtures/babel/babel-single-quotes.js');
expect(transpiled(3)).toBe(4);
}));
describe('when a .js file starts with "use babel";', () =>
it('transpiles it using babel', function() {
const transpiled = require('./fixtures/babel/babel-double-quotes.js');
expect(transpiled(3)).toBe(4);
}));
describe('when a .js file starts with /* @flow */', () =>
it('transpiles it using babel', function() {
const transpiled = require('./fixtures/babel/flow-comment.js');
expect(transpiled(3)).toBe(4);
}));
describe('when a .js file starts with // @flow', () =>
it('transpiles it using babel', function() {
const transpiled = require('./fixtures/babel/flow-slash-comment.js');
expect(transpiled(3)).toBe(4);
}));
describe("when a .js file does not start with 'use babel';", function() {
it('does not transpile it using babel', function() {
spyOn(console, 'error');
expect(() => require('./fixtures/babel/invalid.js')).toThrow();
});
it('does not try to log to stdout or stderr while parsing the file', function() {
spyOn(process.stderr, 'write');
spyOn(process.stdout, 'write');
require('./fixtures/babel/babel-double-quotes.js');
expect(process.stdout.write).not.toHaveBeenCalled();
expect(process.stderr.write).not.toHaveBeenCalled();
});
});
});