Skip to content

Commit eca59d9

Browse files
committed
Switch to ESM (fixes #406)
1 parent d73c315 commit eca59d9

20 files changed

+47
-29
lines changed

Diff for: README.md

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ You can also import the app factory and create your own app:
7373

7474
```js
7575
import appFactory from "fauxauth";
76-
// or `const { default: appFactory } = require("fauxauth");`
7776

7877
const app = appFactory({ callbackUrl: "http://localhost:3001" });
7978

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
"build": "npm --workspace packages/fauxauth run build",
1111
"build:docker": "docker build . --build-arg ALPINE_RELEASE=3.16 --build-arg NODE_RELEASE=$(cat .nvmrc) --tag textbook/fauxauth",
1212
"e2e": "npm --workspace packages/e2e run e2e",
13+
"e2e:custom": "npm --workspace packages/e2e run custom-e2e",
1314
"pree2e:docker": "npm run build:docker",
1415
"e2e:docker": "cross-env NODE_RELEASE=$(cat .nvmrc) TAG=latest npm --workspace packages/e2e run docker",
1516
"install:chromedriver": "npm --workspace packages/e2e run chromedriver",
1617
"lint": "eslint --ext=js,ts .",
17-
"ship": "npm run lint && npm run test && npm run build && npm run e2e",
18+
"ship": "npm run lint && npm run test && npm run build && npm run e2e && npm run e2e:custom",
1819
"test": "npm --workspace packages/fauxauth test"
1920
},
2021
"repository": {

Diff for: packages/e2e/fauxauth.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const axios = require("axios");
2-
const { format, parse, URLSearchParams } = require("url");
3-
const { remote } = require("webdriverio");
1+
import axios from "axios";
2+
import { format, parse, URLSearchParams } from "url";
3+
import { remote } from "webdriverio";
44

55
const baseUrl = process.env.FAUXAUTH_URL || "http://localhost:3000";
66

Diff for: packages/e2e/jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
module.exports = {
1+
export default {
22
reporters: [
33
"default",
44
["jest-junit", { outputDirectory: "./reports/jest" }],
55
],
66
testTimeout: parseInt(process.env.JEST_TIMEOUT , 10) || 30_000,
7+
transform: {},
78
};

Diff for: packages/e2e/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"description": "E2E tests for fauxauth",
66
"main": "index.js",
7+
"type": "module",
78
"devDependencies": {
89
"axios": "^1.1.3",
910
"concurrently": "^7.5.0",
@@ -17,6 +18,7 @@
1718
},
1819
"scripts": {
1920
"chromedriver": "webdriver-manager update --gecko=false --versions.chrome=${CHROME_VERSION:-latest}",
21+
"custom-e2e": "concurrently --kill-others --names app,run,sln --success first \"node server.js\" \"npm:e2e:run\" \"npm:e2e:sln\"",
2022
"predocker": "docker-compose build",
2123
"docker": "docker-compose run e2e || (npm run postdocker && exit 1)",
2224
"postdocker": "docker-compose logs fauxauth selenium && docker-compose down",
@@ -25,7 +27,7 @@
2527
"e2e:run": "npm run test",
2628
"e2e:sln": "webdriver-manager start",
2729
"pretest": "wait-on --timeout 30000 --log \"http-get://${SELENIUM_HOST:-localhost}:4444${SELENIUM_PATH:-/wd/hub}\"",
28-
"test": "jest"
30+
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
2931
},
3032
"repository": {
3133
"type": "git",

Diff for: packages/e2e/server.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import appFactory from "fauxauth";
2+
3+
const app = appFactory({});
4+
const port = parseInt(process.env.PORT ?? "3000", 10);
5+
6+
// eslint-disable-next-line no-console
7+
app.listen(port, () => console.log(`listening on ${port}`));

Diff for: packages/fauxauth/jest.config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import { Config } from "@jest/types";
22

33
const config: Config.InitialOptions = {
44
coverageDirectory: "./coverage",
5-
preset: "ts-jest",
5+
extensionsToTreatAsEsm: [".ts"],
6+
moduleNameMapper: {
7+
"^(\\.{1,2}/.*)\\.js$": "$1",
8+
},
9+
preset: "ts-jest/presets/default-esm",
610
reporters: [
711
"default",
812
["jest-junit", { outputDirectory: "./reports/jest" }],
913
],
14+
transform: {
15+
"^.+\\.ts$": ["ts-jest", { useESM: true }],
16+
},
1017
};
1118

1219
export default config;

Diff for: packages/fauxauth/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"main": "lib/index.js",
99
"types": "lib/index.d.ts",
10+
"type": "module",
1011
"files": [
1112
"/lib",
1213
"/views"
@@ -19,7 +20,7 @@
1920
"build": "tsc --project tsconfig.build.json",
2021
"postbuild": "chmod u+x ./lib/server.js && cpy ../../README.md packages/fauxauth/",
2122
"start": "node $npm_package_bin_fauxauth",
22-
"test": "jest",
23+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
2324
"test:cover": "npm run test -- --coverage",
2425
"test:watch": "npm run test -- --watch"
2526
},

Diff for: packages/fauxauth/src/accessToken.integration.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import request from "supertest";
33
import { parse } from "url";
44
import { parseString } from "xml2js";
55

6-
import appFactory, { Configuration } from "./index";
6+
import appFactory, { Configuration } from "./index.js";
77

88
describe("access_token endpoint", () => {
99
let app: Application;

Diff for: packages/fauxauth/src/app.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import cors from "cors";
22
import express, { Application } from "express";
33
import morgan from "morgan";
4-
import path from "path";
4+
import url from "url";
55

6-
import { Configuration, initialise } from "./config";
7-
import tokenRouter from "./routes/accessToken";
8-
import authRouter from "./routes/authorize";
9-
import configRouter from "./routes/configuration";
6+
import { Configuration, initialise } from "./config.js";
7+
import tokenRouter from "./routes/accessToken.js";
8+
import authRouter from "./routes/authorize.js";
9+
import configRouter from "./routes/configuration.js";
1010

1111
export default (overrides?: Partial<Configuration>): Application => {
1212
const app = express();
1313
initialise(overrides);
1414

1515
app.set("view engine", "pug");
16-
app.set("views", path.join(__dirname, "..", "views"));
16+
app.set("views", url.fileURLToPath(new URL("../views", import.meta.url)));
1717
app.use(cors());
1818
app.use(express.json());
1919
app.use(express.urlencoded({ extended: false }));

Diff for: packages/fauxauth/src/authorize.integration.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Application } from "express";
22
import request from "supertest";
33
import { format, parse } from "url";
44

5-
import appFactory, { Configuration } from "./index";
5+
import appFactory, { Configuration } from "./index.js";
66

77
describe("authorize endpoint", () => {
88
const endpoint = "/authorize";

Diff for: packages/fauxauth/src/configuration.integration.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Application } from "express";
22
import request from "supertest";
33

4-
import appFactory, { Configuration } from "./index";
4+
import appFactory, { Configuration } from "./index.js";
55

66
describe("_configure endpoint", () => {
77
const endpoint = "/_configuration";

Diff for: packages/fauxauth/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import appFactory from "./app";
1+
import appFactory from "./app.js";
22
export default appFactory;
33

4-
export { Configuration } from "./config";
4+
export type { Configuration } from "./config.js";

Diff for: packages/fauxauth/src/routes/accessToken.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import debug from "debug";
22
import { Request, Response, Router } from "express";
33
import { Builder } from "xml2js";
44

5-
import { getAll } from "../config";
5+
import { getAll } from "../config.js";
66

77
const log = debug("fauxauth:accessToken");
88

Diff for: packages/fauxauth/src/routes/authorize.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateRedirect } from "./authorize";
1+
import { validateRedirect } from "./authorize.js";
22

33
describe("validateRedirect function", () => {
44
const callback = "http://example.com/path";

Diff for: packages/fauxauth/src/routes/authorize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Request, Response, Router } from "express";
33
import { ParsedUrlQueryInput } from "querystring";
44
import { format, URL } from "url";
55

6-
import { getAll } from "../config";
7-
import { generateHex } from "../utils";
6+
import { getAll } from "../config.js";
7+
import { generateHex } from "../utils.js";
88

99
const log = debug("fauxauth:authorize");
1010

Diff for: packages/fauxauth/src/routes/configuration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request, Response, Router } from "express";
22
import jiff from "jiff";
33

4-
import { Configuration, getAll, reset, update } from "../config";
4+
import { Configuration, getAll, reset, update } from "../config.js";
55

66
const router = Router();
77

Diff for: packages/fauxauth/src/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import http from "http";
44

5-
import appFactory from ".";
5+
import appFactory from "./index.js";
66

77
const port = parseInt(process.env.PORT || "3000", 10);
88

Diff for: packages/fauxauth/src/utils.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateHex } from "./utils";
1+
import { generateHex } from "./utils.js";
22

33
describe("generateHex function", () => {
44
it("generates valid hex strings", () => {

Diff for: packages/fauxauth/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2828

2929
/* Modules */
30-
"module": "commonjs", /* Specify what module code is generated. */
30+
"module": "es2020", /* Specify what module code is generated. */
3131
// "rootDir": "./", /* Specify the root folder within your source files. */
32-
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
32+
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
3333
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3434
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3535
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */

0 commit comments

Comments
 (0)