babel-spec.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Users may have this environment variable set. Currently, it causes babel to
  2. // log to stderr, which causes errors on Windows.
  3. // See https://github.com/atom/electron/issues/2033
  4. process.env.DEBUG = '*';
  5. const path = require('path');
  6. const temp = require('temp').track();
  7. const CompileCache = require('../src/compile-cache');
  8. describe('Babel transpiler support', function() {
  9. let originalCacheDir = null;
  10. beforeEach(function() {
  11. originalCacheDir = CompileCache.getCacheDirectory();
  12. CompileCache.setCacheDirectory(temp.mkdirSync('compile-cache'));
  13. // TODO: rework to avoid using IIFE https://developer.mozilla.org/en-US/docs/Glossary/IIFE
  14. return (() => {
  15. const result = [];
  16. for (let cacheKey of Object.keys(require.cache)) {
  17. if (cacheKey.startsWith(path.join(__dirname, 'fixtures', 'babel'))) {
  18. result.push(delete require.cache[cacheKey]);
  19. } else {
  20. result.push(undefined);
  21. }
  22. }
  23. return result;
  24. })();
  25. });
  26. afterEach(function() {
  27. CompileCache.setCacheDirectory(originalCacheDir);
  28. try {
  29. return temp.cleanupSync();
  30. } catch (error) {}
  31. });
  32. describe('when a .js file starts with /** @babel */;', () =>
  33. it('transpiles it using babel', function() {
  34. const transpiled = require('./fixtures/babel/babel-comment.js');
  35. expect(transpiled(3)).toBe(4);
  36. }));
  37. describe("when a .js file starts with 'use babel';", () =>
  38. it('transpiles it using babel', function() {
  39. const transpiled = require('./fixtures/babel/babel-single-quotes.js');
  40. expect(transpiled(3)).toBe(4);
  41. }));
  42. describe('when a .js file starts with "use babel";', () =>
  43. it('transpiles it using babel', function() {
  44. const transpiled = require('./fixtures/babel/babel-double-quotes.js');
  45. expect(transpiled(3)).toBe(4);
  46. }));
  47. describe('when a .js file starts with /* @flow */', () =>
  48. it('transpiles it using babel', function() {
  49. const transpiled = require('./fixtures/babel/flow-comment.js');
  50. expect(transpiled(3)).toBe(4);
  51. }));
  52. describe('when a .js file starts with // @flow', () =>
  53. it('transpiles it using babel', function() {
  54. const transpiled = require('./fixtures/babel/flow-slash-comment.js');
  55. expect(transpiled(3)).toBe(4);
  56. }));
  57. describe("when a .js file does not start with 'use babel';", function() {
  58. it('does not transpile it using babel', function() {
  59. spyOn(console, 'error');
  60. expect(() => require('./fixtures/babel/invalid.js')).toThrow();
  61. });
  62. it('does not try to log to stdout or stderr while parsing the file', function() {
  63. spyOn(process.stderr, 'write');
  64. spyOn(process.stdout, 'write');
  65. require('./fixtures/babel/babel-double-quotes.js');
  66. expect(process.stdout.write).not.toHaveBeenCalled();
  67. expect(process.stderr.write).not.toHaveBeenCalled();
  68. });
  69. });
  70. });