Last active
August 24, 2022 10:12
-
-
Save smeijer/deed17dac8b084342e06300f4903cd5a to your computer and use it in GitHub Desktop.
Testing build output for a node lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-var-requires */ | |
import { transform } from '@babel/core'; | |
import { exec } from 'child_process'; | |
import { readFileSync } from 'fs'; | |
import { promisify } from 'util'; | |
const execAsync = promisify(exec); | |
const pkg = require('../../package.json'); | |
function importBundle(filepath: string) { | |
const source = readFileSync(filepath, { encoding: 'utf-8' }); | |
try { | |
const module = require(`../../${filepath}`); | |
return { source, module }; | |
} catch {} | |
const commonjs = transform(source, { | |
babelrc: false, | |
compact: false, | |
plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')], | |
}); | |
const module = eval(`(function() { ${commonjs?.code}; return exports; })()`); | |
return { source, module }; | |
} | |
beforeAll(async () => { | |
if (process.env.WALLABY_ENV && existsSync('./dist')) return; | |
jest.setTimeout(60_000); | |
await execAsync('yarn build'); | |
}); | |
test('creates all bundles', () => { | |
expect(pkg.main).toEqual('dist/magicbell-react-headless.js'); | |
expect(pkg.module).toEqual('dist/magicbell-react-headless.esm.js'); | |
}); | |
test('can import functions from main module', () => { | |
const { source, module } = importBundle(pkg.main); | |
expect(source).toMatch(/require\("react"\)/i); | |
expect(source).not.toMatch(/import.*from\s?"react"/i); | |
expect(typeof module.useConfig).toEqual('function'); | |
}); | |
test('can import functions from esm module', async () => { | |
const { source, module } = importBundle(pkg.module); | |
expect(source).toMatch(/import.*from\s?"react"/i); | |
expect(source).not.toMatch(/require\("react"\)/i); | |
expect(typeof module.useConfig).toEqual('function'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment