This repository was archived by the owner on Apr 18, 2024. It is now read-only.
forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
91 lines (84 loc) · 2.31 KB
/
Copy pathtest.js
File metadata and controls
91 lines (84 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Based on https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/test.js
*/
"use strict";
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = "test";
process.env.NODE_ENV = "test";
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on("unhandledRejection", (err) => {
throw err;
});
const jest = require("jest");
const path = require("path");
const paths = require("./util/paths");
const createJestConfig = require("./util/createJestConfig");
const { Util } = require("@serverless-stack/core");
let argv = process.argv.slice(2);
argv.push(
"--config",
JSON.stringify(
createJestConfig(
(relativePath) => path.resolve(__dirname, "..", relativePath),
path.resolve(paths.appPath)
)
)
);
// Load environment variables from dotenv
Util.Environment.load();
// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
// We're trying to resolve the environment ourselves because Jest does it incorrectly.
// TODO: remove this as soon as it's fixed in Jest.
const resolve = require("resolve");
function resolveJestDefaultEnvironment(name) {
const jestDir = path.dirname(
resolve.sync("jest", {
basedir: __dirname,
})
);
const jestCLIDir = path.dirname(
resolve.sync("jest-cli", {
basedir: jestDir,
})
);
const jestConfigDir = path.dirname(
resolve.sync("jest-config", {
basedir: jestCLIDir,
})
);
return resolve.sync(name, {
basedir: jestConfigDir,
});
}
let cleanArgv = [];
let env = "jsdom";
let next;
do {
next = argv.shift();
if (next === "--env") {
env = argv.shift();
} else if (next.indexOf("--env=") === 0) {
env = next.substring("--env=".length);
} else {
cleanArgv.push(next);
}
} while (argv.length > 0);
argv = cleanArgv;
let resolvedEnv;
try {
resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
} catch (e) {
// ignore
}
if (!resolvedEnv) {
try {
resolvedEnv = resolveJestDefaultEnvironment(env);
} catch (e) {
// ignore
}
}
const testEnvironment = resolvedEnv || env;
argv.push("--env", testEnvironment);
jest.run(argv);