|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +jest.mock('@lwc/compiler/dist/index.cjs', () => ({ |
| 8 | + transformSync: jest.fn(() => ({ |
| 9 | + code: '/* lwc-compiled */', |
| 10 | + map: null, |
| 11 | + warnings: [], |
| 12 | + })), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@babel/core', () => ({ |
| 16 | + transform: jest.fn(() => ({ code: '/* babel */' })), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@lwc/template-compiler/dist/index.cjs', () => ({ |
| 20 | + generateScopeTokens: jest.fn(() => ({ cssScopeTokens: undefined })), |
| 21 | +})); |
| 22 | + |
| 23 | +const lwcCompiler = require('@lwc/compiler/dist/index.cjs'); |
| 24 | +const { transformLwc } = require('../index'); |
| 25 | + |
| 26 | +describe('transformLwc compiler options (namespace / enablePrivateMethods)', () => { |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('sets enablePrivateMethods true when path matches modules/... (lightning)', () => { |
| 32 | + transformLwc('export default class {}', '/repo/modules/lightning/foo/foo.js', false); |
| 33 | + |
| 34 | + expect(lwcCompiler.transformSync).toHaveBeenCalledWith( |
| 35 | + expect.any(String), |
| 36 | + '/repo/modules/lightning/foo/foo.js', |
| 37 | + expect.objectContaining({ |
| 38 | + namespace: 'x', |
| 39 | + enablePrivateMethods: true, |
| 40 | + }) |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('sets enablePrivateMethods true when path matches modules/... (interop)', () => { |
| 45 | + transformLwc('export default class {}', '/repo/modules/interop/bar/bar.js', false); |
| 46 | + |
| 47 | + expect(lwcCompiler.transformSync).toHaveBeenCalledWith( |
| 48 | + expect.any(String), |
| 49 | + '/repo/modules/interop/bar/bar.js', |
| 50 | + expect.objectContaining({ |
| 51 | + namespace: 'x', |
| 52 | + enablePrivateMethods: true, |
| 53 | + }) |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('sets enablePrivateMethods false when path matches modules/smoke/...', () => { |
| 58 | + transformLwc( |
| 59 | + 'export default class {}', |
| 60 | + '/repo/modules/smoke/privateMethods/privateMethods.js', |
| 61 | + false |
| 62 | + ); |
| 63 | + |
| 64 | + expect(lwcCompiler.transformSync).toHaveBeenCalledWith( |
| 65 | + expect.any(String), |
| 66 | + '/repo/modules/smoke/privateMethods/privateMethods.js', |
| 67 | + expect.objectContaining({ |
| 68 | + namespace: 'x', |
| 69 | + enablePrivateMethods: false, |
| 70 | + }) |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('sets enablePrivateMethods false for other modules/... namespaces', () => { |
| 75 | + transformLwc('export default class {}', '/repo/modules/c/baz/baz.js', false); |
| 76 | + |
| 77 | + expect(lwcCompiler.transformSync).toHaveBeenCalledWith( |
| 78 | + expect.any(String), |
| 79 | + '/repo/modules/c/baz/baz.js', |
| 80 | + expect.objectContaining({ |
| 81 | + namespace: 'x', |
| 82 | + enablePrivateMethods: false, |
| 83 | + }) |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('sets enablePrivateMethods false when path has no modules/ or jest-modules/ segment', () => { |
| 88 | + transformLwc('export default class {}', '/repo/src/standalone.js', false); |
| 89 | + |
| 90 | + expect(lwcCompiler.transformSync).toHaveBeenCalledWith( |
| 91 | + expect.any(String), |
| 92 | + '/repo/src/standalone.js', |
| 93 | + expect.objectContaining({ |
| 94 | + namespace: 'x', |
| 95 | + enablePrivateMethods: false, |
| 96 | + }) |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
0 commit comments