From 6c79f9eb3d3f855d533b56cdd28fd92ececf96a2 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Mon, 29 Jan 2024 22:13:15 -0500 Subject: [PATCH] feat: switch to esm --- .eslintignore | 1 + .eslintrc.js => .eslintrc.cjs | 0 jest.config.js | 17 ++++++------ package.json | 1 + rollup.config.mjs => rollup.config.js | 0 site/{rollup.config.mjs => rollup.config.js} | 0 .../{babel.config.js => babel.config.cjs} | 0 test-runtime/puppeteer_environment.js | 16 +++++------- test-runtime/setup.js | 26 ++++++++++--------- test-runtime/teardown.js | 14 +++++----- 10 files changed, 39 insertions(+), 36 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) rename rollup.config.mjs => rollup.config.js (100%) rename site/{rollup.config.mjs => rollup.config.js} (100%) rename test-runtime/{babel.config.js => babel.config.cjs} (100%) diff --git a/.eslintignore b/.eslintignore index e8ddab3480..d3623326ac 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ build +*.cjs *.js diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/jest.config.js b/jest.config.js index b98fab0a01..ace3d301b0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,16 +1,17 @@ /** @type {import('jest').Config} */ -const config = { +export default { preset: 'ts-jest/presets/default-esm', testPathIgnorePatterns: ['/node_modules', '/build', '/_site', '/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; diff --git a/package.json b/package.json index e09b3914f9..37d22dd840 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.mjs b/rollup.config.js similarity index 100% rename from rollup.config.mjs rename to rollup.config.js diff --git a/site/rollup.config.mjs b/site/rollup.config.js similarity index 100% rename from site/rollup.config.mjs rename to site/rollup.config.js diff --git a/test-runtime/babel.config.js b/test-runtime/babel.config.cjs similarity index 100% rename from test-runtime/babel.config.js rename to test-runtime/babel.config.cjs diff --git a/test-runtime/puppeteer_environment.js b/test-runtime/puppeteer_environment.js index 808a34d029..44bfb63224 100644 --- a/test-runtime/puppeteer_environment.js +++ b/test-runtime/puppeteer_environment.js @@ -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); } @@ -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 }); } @@ -36,5 +36,3 @@ class PuppeteerEnvironment extends NodeEnvironment { return super.getVmContext(); } } - -module.exports = PuppeteerEnvironment; diff --git a/test-runtime/setup.js b/test-runtime/setup.js index 825353b25e..088552118d 100644 --- a/test-runtime/setup.js +++ b/test-runtime/setup.js @@ -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()); +} diff --git a/test-runtime/teardown.js b/test-runtime/teardown.js index 440af9883a..c8a53be5c6 100644 --- a/test-runtime/teardown.js +++ b/test-runtime/teardown.js @@ -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}); +}