From c888e6583f0bd64516c23945ff6a740615b10279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Bigio?= Date: Thu, 21 Jan 2016 09:09:52 -0800 Subject: [PATCH] Unit test `worker.js` Summary: public We had a bug a few weeks ago which caused transform errors not to be shown properly. The problem was on this piece of code, so lets unit test it. Reviewed By: davidaurelio Differential Revision: D2849990 fb-gh-sync-id: 9f6bb8a8b7d59bbaa3577c29cee37fb23a50d66f --- .../JSTransformer/__tests__/worker-test.js | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 packager/react-packager/src/JSTransformer/__tests__/worker-test.js diff --git a/packager/react-packager/src/JSTransformer/__tests__/worker-test.js b/packager/react-packager/src/JSTransformer/__tests__/worker-test.js new file mode 100644 index 000000000..fdb67d9d4 --- /dev/null +++ b/packager/react-packager/src/JSTransformer/__tests__/worker-test.js @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +jest.autoMockOff(); + +jest.mock('babel-core'); + +const worker = require('../worker'); +const babel = require('babel-core'); + +const code = 'code'; + +describe('Resolver', function() { + beforeEach(() => { + babel.transform.mockImpl((source, options) => source); + }); + + describe('when no external transform is provided', () => { + it('should invoke internal transform if available', () => { + transform({ + sourceCode: 'code', + filename: 'test', + options: options({opts: {}, internalTransformsEnabled: true}), + }); + expect(babel.transform.mock.calls.length).toBe(1); + }); + + it('should not invoke internal transform if unavailable', () => { + transform({ + sourceCode: 'code', + filename: 'test', + options: options({opts: {}, internalTransformsEnabled: false}), + }); + expect(babel.transform.mock.calls.length).toBe(0); + }); + }); + + describe('when external transform is provided', () => { + it('should invoke both transformers if internal is available', () => { + transform({ + sourceCode: code, + filename: 'test', + options: options({ + opts: { + externalTransformModulePath: require.resolve( + '../../../../transformer.js' + ), + }, + internalTransformsEnabled: true, + }), + }); + expect(babel.transform.mock.calls.length).toBe(2); + }); + + it('should invoke only external transformer if internal is not available', () => { + transform({ + sourceCode: 'code', + filename: 'test', + options: options({ + opts: { + externalTransformModulePath: require.resolve( + '../../../../transformer.js' + ), + }, + internalTransformsEnabled: false, + }), + }); + expect(babel.transform.mock.calls.length).toBe(1); + }); + + it('should pipe errors through transform pipeline', () => { + const error = new Error('transform error'); + babel.transform.mockImpl((source, options) => { + throw error; + }); + + const callback = transform({ + sourceCode: 'code', + filename: 'test', + options: options({ + opts: { + externalTransformModulePath: require.resolve( + '../../../../transformer.js' + ), + }, + internalTransformsEnabled: true, + }), + }); + expect(callback.mock.calls[0][0]).toBe(error); + }); + }); +}); + +function transform(data) { + const callback = jest.genMockFunction(); + worker(data, callback); + return callback; +} + +function options({opts, internalTransformsEnabled}) { + return Object.assign(opts, {hot: internalTransformsEnabled}); +}