Skip to content

Commit

Permalink
feat: switch to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Jan 30, 2024
1 parent ed1944c commit 6c79f9e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 36 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
*.cjs
*.js
File renamed without changes.
17 changes: 9 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/** @type {import('jest').Config} */
const config = {
export default {
preset: 'ts-jest/presets/default-esm',
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/build', '<rootDir>/_site', '<rootDir>/src'],
coverageDirectory: './coverage/',
collectCoverage: false,
setupFiles: ['./test/jest.overrides.ts'],
globals: {
'ts-jest': {
diagnostics: false,
useESM: true
}
transform: {
'^.+\\.ts$': [
'ts-jest',
{
diagnostics: false,
useESM: true
}
]
}
};

module.exports = config;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jsdelivr": "build/vega-lite.min.js",
"module": "build/src/index",
"types": "build/src/index.d.ts",
"type": "module",
"bin": {
"vl2pdf": "./bin/vl2pdf",
"vl2png": "./bin/vl2png",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 7 additions & 9 deletions test-runtime/puppeteer_environment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const {readFile} = require('fs').promises;
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer');
const NodeEnvironment = require('jest-environment-node').TestEnvironment;
import {readFile} from 'fs/promises';
import os from 'os';
import path from 'path';
import {connect} from 'puppeteer';
import NodeEnvironment from 'jest-environment-node';

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

class PuppeteerEnvironment extends NodeEnvironment {
export default class PuppeteerEnvironment extends NodeEnvironment.TestEnvironment {
constructor(config) {
super(config);
}
Expand All @@ -20,7 +20,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
}

// connect to puppeteer
this.global.__BROWSER_GLOBAL__ = await puppeteer.connect({
this.global.__BROWSER_GLOBAL__ = await connect({
browserWSEndpoint: wsEndpoint
});
}
Expand All @@ -36,5 +36,3 @@ class PuppeteerEnvironment extends NodeEnvironment {
return super.getVmContext();
}
}

module.exports = PuppeteerEnvironment;
26 changes: 14 additions & 12 deletions test-runtime/setup.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const {mkdir, writeFile} = require('fs').promises;
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer');
const {setup: setupDevServer} = require('jest-dev-server');
const chalk = require('chalk');
import {mkdir, writeFile} from 'fs/promises';
import {tmpdir} from 'os';
import {join} from 'path';
import {launch} from 'puppeteer';
import {setup as setupDevServer} from 'jest-dev-server';
import chalk from 'chalk';

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
const {green} = chalk;

module.exports = async function () {
const DIR = join(tmpdir(), 'jest_puppeteer_global_setup');

export default async function () {
globalThis.servers = await setupDevServer({
command: `node ./node_modules/.bin/serve -l 8000`,
launchTimeout: 50000,
port: 8000
});

console.log(chalk.green('Setup Puppeteer'));
console.log(green('Setup Puppeteer'));

const browser = await puppeteer.launch();
const browser = await launch();
// store the browser instance so we can teardown it later
// this global is only available in the teardown but not in TestEnvironments
globalThis.__BROWSER_GLOBAL__ = browser;

// use the file system to expose the wsEndpoint for TestEnvironments
await mkdir(DIR, {recursive: true});
await writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
};
await writeFile(join(DIR, 'wsEndpoint'), browser.wsEndpoint());
}
14 changes: 7 additions & 7 deletions test-runtime/teardown.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const fs = require('fs').promises;
const os = require('os');
const path = require('path');
const {teardown: teardownDevServer} = require('jest-dev-server');
import {rm} from 'fs/promises';
import os from 'os';
import path from 'path';
import {teardown as teardownDevServer} from 'jest-dev-server';

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

module.exports = async function () {
export default async function () {
await teardownDevServer(globalThis.servers);

// close the browser instance
await globalThis.__BROWSER_GLOBAL__.close();

// clean-up the wsEndpoint file
await fs.rm(DIR, {recursive: true, force: true});
};
await rm(DIR, {recursive: true, force: true});
}

0 comments on commit 6c79f9e

Please sign in to comment.