@cedarjs/testing 0.6.1-next.0 → 0.6.1-next.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/config/jest/api/RedwoodApiJestEnv.d.ts +11 -0
  2. package/config/jest/api/RedwoodApiJestEnv.d.ts.map +1 -0
  3. package/config/jest/api/RedwoodApiJestEnv.js +33 -16
  4. package/config/jest/api/apiBabelConfig.d.ts +5 -0
  5. package/config/jest/api/apiBabelConfig.d.ts.map +1 -0
  6. package/config/jest/api/apiBabelConfig.js +33 -17
  7. package/config/jest/api/globalSetup.d.ts +3 -0
  8. package/config/jest/api/globalSetup.d.ts.map +1 -0
  9. package/config/jest/api/globalSetup.js +59 -44
  10. package/config/jest/api/index.d.ts +3 -0
  11. package/config/jest/api/index.d.ts.map +1 -0
  12. package/config/jest/api/index.js +25 -2
  13. package/config/jest/api/jest-preset.d.ts +4 -0
  14. package/config/jest/api/jest-preset.d.ts.map +1 -0
  15. package/config/jest/api/jest-preset.js +69 -39
  16. package/config/jest/api/jest.setup.d.ts +2 -0
  17. package/config/jest/api/jest.setup.d.ts.map +1 -0
  18. package/config/jest/api/jest.setup.js +189 -248
  19. package/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts +11 -0
  20. package/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts.map +1 -0
  21. package/config/jest/babelPlugins/babel-plugin-redwood-cell.js +103 -124
  22. package/config/jest/jest-serial-runner.d.ts +8 -0
  23. package/config/jest/jest-serial-runner.d.ts.map +1 -0
  24. package/config/jest/jest-serial-runner.js +33 -9
  25. package/config/jest/web/RedwoodWebJestEnv.d.ts +6 -0
  26. package/config/jest/web/RedwoodWebJestEnv.d.ts.map +1 -0
  27. package/config/jest/web/RedwoodWebJestEnv.js +33 -12
  28. package/config/jest/web/index.d.ts +3 -0
  29. package/config/jest/web/index.d.ts.map +1 -0
  30. package/config/jest/web/index.js +25 -2
  31. package/config/jest/web/jest-preset.d.ts +4 -0
  32. package/config/jest/web/jest-preset.d.ts.map +1 -0
  33. package/config/jest/web/jest-preset.js +78 -50
  34. package/config/jest/web/jest.setup.d.ts +8 -0
  35. package/config/jest/web/jest.setup.d.ts.map +1 -0
  36. package/config/jest/web/jest.setup.js +17 -36
  37. package/config/jest/web/resolver.d.ts +4 -0
  38. package/config/jest/web/resolver.d.ts.map +1 -0
  39. package/config/jest/web/resolver.js +24 -31
  40. package/config/jest/web/webBabelConfig.d.ts +10 -0
  41. package/config/jest/web/webBabelConfig.d.ts.map +1 -0
  42. package/config/jest/web/webBabelConfig.js +43 -13
  43. package/config/package.json +1 -0
  44. package/dist/api/directUrlHelpers.d.ts.map +1 -1
  45. package/dist/api/index.d.ts +1 -0
  46. package/dist/api/index.d.ts.map +1 -1
  47. package/dist/api/index.js +1 -0
  48. package/dist/cjs/api/directUrlHelpers.d.ts.map +1 -1
  49. package/dist/cjs/api/index.d.ts +1 -0
  50. package/dist/cjs/api/index.d.ts.map +1 -1
  51. package/dist/cjs/api/index.js +3 -1
  52. package/dist/cjs/config/jest/api/RedwoodApiJestEnv.d.ts +11 -0
  53. package/dist/cjs/config/jest/api/RedwoodApiJestEnv.d.ts.map +1 -0
  54. package/dist/cjs/config/jest/api/RedwoodApiJestEnv.js +48 -0
  55. package/dist/cjs/config/jest/api/apiBabelConfig.d.ts +5 -0
  56. package/dist/cjs/config/jest/api/apiBabelConfig.d.ts.map +1 -0
  57. package/dist/cjs/config/jest/api/apiBabelConfig.js +34 -0
  58. package/dist/cjs/config/jest/api/globalSetup.d.ts +3 -0
  59. package/dist/cjs/config/jest/api/globalSetup.d.ts.map +1 -0
  60. package/dist/cjs/config/jest/api/globalSetup.js +61 -0
  61. package/dist/cjs/config/jest/api/index.d.ts +3 -0
  62. package/dist/cjs/config/jest/api/index.d.ts.map +1 -0
  63. package/dist/cjs/config/jest/api/index.js +25 -0
  64. package/dist/cjs/config/jest/api/jest-preset.d.ts +4 -0
  65. package/dist/cjs/config/jest/api/jest-preset.d.ts.map +1 -0
  66. package/dist/cjs/config/jest/api/jest-preset.js +97 -0
  67. package/dist/cjs/config/jest/api/jest.setup.d.ts +2 -0
  68. package/dist/cjs/config/jest/api/jest.setup.d.ts.map +1 -0
  69. package/dist/cjs/config/jest/api/jest.setup.js +267 -0
  70. package/dist/cjs/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts +11 -0
  71. package/dist/cjs/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts.map +1 -0
  72. package/dist/cjs/config/jest/babelPlugins/babel-plugin-redwood-cell.js +121 -0
  73. package/dist/cjs/config/jest/jest-serial-runner.d.ts +8 -0
  74. package/dist/cjs/config/jest/jest-serial-runner.d.ts.map +1 -0
  75. package/dist/cjs/config/jest/jest-serial-runner.js +37 -0
  76. package/dist/cjs/config/jest/web/RedwoodWebJestEnv.d.ts +6 -0
  77. package/dist/cjs/config/jest/web/RedwoodWebJestEnv.d.ts.map +1 -0
  78. package/dist/cjs/config/jest/web/RedwoodWebJestEnv.js +38 -0
  79. package/dist/cjs/config/jest/web/index.d.ts +3 -0
  80. package/dist/cjs/config/jest/web/index.d.ts.map +1 -0
  81. package/dist/cjs/config/jest/web/index.js +25 -0
  82. package/dist/cjs/config/jest/web/jest-preset.d.ts +4 -0
  83. package/dist/cjs/config/jest/web/jest-preset.d.ts.map +1 -0
  84. package/dist/cjs/config/jest/web/jest-preset.js +117 -0
  85. package/dist/cjs/config/jest/web/jest.setup.d.ts +8 -0
  86. package/dist/cjs/config/jest/web/jest.setup.d.ts.map +1 -0
  87. package/dist/cjs/config/jest/web/jest.setup.js +22 -0
  88. package/dist/cjs/config/jest/web/resolver.d.ts +4 -0
  89. package/dist/cjs/config/jest/web/resolver.d.ts.map +1 -0
  90. package/dist/cjs/config/jest/web/resolver.js +30 -0
  91. package/dist/cjs/config/jest/web/webBabelConfig.d.ts +10 -0
  92. package/dist/cjs/config/jest/web/webBabelConfig.d.ts.map +1 -0
  93. package/dist/cjs/config/jest/web/webBabelConfig.js +48 -0
  94. package/dist/cjs/web/global.d.ts +2 -2
  95. package/dist/cjs/web/global.d.ts.map +1 -1
  96. package/dist/config/jest/api/RedwoodApiJestEnv.d.ts +11 -0
  97. package/dist/config/jest/api/RedwoodApiJestEnv.d.ts.map +1 -0
  98. package/dist/config/jest/api/RedwoodApiJestEnv.js +28 -0
  99. package/dist/config/jest/api/apiBabelConfig.d.ts +5 -0
  100. package/dist/config/jest/api/apiBabelConfig.d.ts.map +1 -0
  101. package/dist/config/jest/api/apiBabelConfig.js +18 -0
  102. package/dist/config/jest/api/globalSetup.d.ts +3 -0
  103. package/dist/config/jest/api/globalSetup.d.ts.map +1 -0
  104. package/dist/config/jest/api/globalSetup.js +34 -0
  105. package/dist/config/jest/api/index.d.ts +3 -0
  106. package/dist/config/jest/api/index.d.ts.map +1 -0
  107. package/dist/config/jest/api/index.js +11 -0
  108. package/dist/config/jest/api/jest-preset.d.ts +4 -0
  109. package/dist/config/jest/api/jest-preset.d.ts.map +1 -0
  110. package/dist/config/jest/api/jest-preset.js +67 -0
  111. package/dist/config/jest/api/jest.setup.d.ts +2 -0
  112. package/dist/config/jest/api/jest.setup.d.ts.map +1 -0
  113. package/dist/config/jest/api/jest.setup.js +244 -0
  114. package/dist/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts +11 -0
  115. package/dist/config/jest/babelPlugins/babel-plugin-redwood-cell.d.ts.map +1 -0
  116. package/dist/config/jest/babelPlugins/babel-plugin-redwood-cell.js +101 -0
  117. package/dist/config/jest/jest-serial-runner.d.ts +8 -0
  118. package/dist/config/jest/jest-serial-runner.d.ts.map +1 -0
  119. package/dist/config/jest/jest-serial-runner.js +23 -0
  120. package/dist/config/jest/web/RedwoodWebJestEnv.d.ts +6 -0
  121. package/dist/config/jest/web/RedwoodWebJestEnv.d.ts.map +1 -0
  122. package/dist/config/jest/web/RedwoodWebJestEnv.js +18 -0
  123. package/dist/config/jest/web/index.d.ts +3 -0
  124. package/dist/config/jest/web/index.d.ts.map +1 -0
  125. package/dist/config/jest/web/index.js +11 -0
  126. package/dist/config/jest/web/jest-preset.d.ts +4 -0
  127. package/dist/config/jest/web/jest-preset.d.ts.map +1 -0
  128. package/dist/config/jest/web/jest-preset.js +87 -0
  129. package/dist/config/jest/web/jest.setup.d.ts +8 -0
  130. package/dist/config/jest/web/jest.setup.d.ts.map +1 -0
  131. package/dist/config/jest/web/jest.setup.js +28 -0
  132. package/dist/config/jest/web/resolver.d.ts +4 -0
  133. package/dist/config/jest/web/resolver.d.ts.map +1 -0
  134. package/dist/config/jest/web/resolver.js +38 -0
  135. package/dist/config/jest/web/webBabelConfig.d.ts +10 -0
  136. package/dist/config/jest/web/webBabelConfig.d.ts.map +1 -0
  137. package/dist/config/jest/web/webBabelConfig.js +18 -0
  138. package/dist/web/global.d.ts +2 -2
  139. package/dist/web/global.d.ts.map +1 -1
  140. package/package.json +20 -11
  141. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-commented-exports/code.js +0 -28
  142. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-commented-exports/output.js +0 -35
  143. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-default-and-other-named-export/code.js +0 -7
  144. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-default-and-other-named-export/output.js +0 -6
  145. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-default-export/code.js +0 -3
  146. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-default-export/output.js +0 -3
  147. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-required-exports/code.js +0 -28
  148. package/config/jest/babelPlugins/__tests__/__fixtures__/cell/cell-with-required-exports/output.js +0 -34
  149. package/config/jest/babelPlugins/__tests__/__fixtures__/viteDistDir/client-build-manifest.json +0 -59
  150. package/config/jest/babelPlugins/__tests__/babel-plugin-redwood-cell.test.ts +0 -11
  151. package/config/jest/package.json +0 -3
@@ -1,326 +1,267 @@
1
- /* eslint-env jest */
2
- // @ts-check
3
-
4
- // @NOTE without these imports in the setup file, mockCurrentUser
5
- // will remain undefined in the user's tests
6
- // Remember to use specific imports
7
- const { defineScenario } = require('@cedarjs/testing/dist/cjs/api/scenario')
8
-
9
- // @NOTE we do this because jest.setup.js runs every time in each context
10
- // while jest-preset runs once. This significantly reduces memory footprint, and testing time
11
- // The key is to reduce the amount of imports in this file, because the require.cache is not shared between each test context
12
- const { apiSrcPath, tearDownCachePath, dbSchemaPath } =
13
- global.__RWJS__TEST_IMPORTS
14
-
15
- global.defineScenario = defineScenario
16
-
17
- // Error codes thrown by [MySQL, SQLite, Postgres] when foreign key constraint
18
- // fails on DELETE
19
- const FOREIGN_KEY_ERRORS = [1451, 1811, 23503]
20
- const TEARDOWN_CACHE_PATH = tearDownCachePath
21
- const DEFAULT_SCENARIO = 'standard'
22
- let teardownOrder = []
23
- let originalTeardownOrder = []
24
-
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var import_node_fs = __toESM(require("node:fs"), 1);
25
+ var import_node_path = __toESM(require("node:path"), 1);
26
+ var import_scenario = require("@cedarjs/testing/dist/cjs/api/scenario.js");
27
+ const { apiSrcPath, tearDownCachePath, dbSchemaPath } = global.__RWJS__TEST_IMPORTS;
28
+ global.defineScenario = import_scenario.defineScenario;
29
+ const mockContextStore = /* @__PURE__ */ new Map();
30
+ const mockContext = new Proxy(
31
+ {},
32
+ {
33
+ get: (_target, prop) => {
34
+ if (prop === "toJSON") {
35
+ return () => mockContextStore.get("context");
36
+ }
37
+ return mockContextStore.get("context")[prop];
38
+ },
39
+ set: (_target, prop, value) => {
40
+ const ctx = mockContextStore.get("context");
41
+ ctx[prop] = value;
42
+ return true;
43
+ }
44
+ }
45
+ );
46
+ global.mockCurrentUser = (currentUser) => {
47
+ mockContextStore.set("context", { currentUser });
48
+ };
49
+ const FOREIGN_KEY_ERRORS = [1451, 1811, 23503];
50
+ const TEARDOWN_CACHE_PATH = tearDownCachePath;
51
+ const DEFAULT_SCENARIO = "standard";
52
+ let teardownOrder = [];
53
+ let originalTeardownOrder = [];
25
54
  const deepCopy = (obj) => {
26
- return JSON.parse(JSON.stringify(obj))
27
- }
28
-
55
+ return JSON.parse(JSON.stringify(obj));
56
+ };
29
57
  const isIdenticalArray = (a, b) => {
30
- return JSON.stringify(a) === JSON.stringify(b)
31
- }
32
-
58
+ return JSON.stringify(a) === JSON.stringify(b);
59
+ };
33
60
  const configureTeardown = async () => {
34
- const { getDMMF, getSchema } = require('@prisma/internals')
35
- const fs = require('fs')
36
-
37
- // @NOTE prisma utils are available in cli lib/schemaHelpers
38
- // But avoid importing them, to prevent memory leaks in jest
39
- const datamodel = await getSchema(dbSchemaPath)
40
- const schema = await getDMMF({ datamodel })
41
- const schemaModels = schema.datamodel.models.map((m) => m.dbName || m.name)
42
-
43
- // check if pre-defined delete order already exists and if so, use it to start
44
- if (fs.existsSync(TEARDOWN_CACHE_PATH)) {
45
- teardownOrder = JSON.parse(fs.readFileSync(TEARDOWN_CACHE_PATH).toString())
61
+ const { getDMMF, getSchema } = require("@prisma/internals");
62
+ const datamodel = await getSchema(dbSchemaPath);
63
+ const schema = await getDMMF({ datamodel });
64
+ const schemaModels = schema.datamodel.models.map(
65
+ (m) => m.dbName || m.name
66
+ );
67
+ if (import_node_fs.default.existsSync(TEARDOWN_CACHE_PATH)) {
68
+ teardownOrder = JSON.parse(import_node_fs.default.readFileSync(TEARDOWN_CACHE_PATH).toString());
46
69
  }
47
-
48
- // check the number of models in case we've added/removed since cache was built
49
70
  if (teardownOrder.length !== schemaModels.length) {
50
- teardownOrder = schemaModels
71
+ teardownOrder = schemaModels;
51
72
  }
52
-
53
- // keep a copy of the original order to compare against
54
- originalTeardownOrder = deepCopy(teardownOrder)
55
- }
56
-
57
- let quoteStyle
58
- // determine what kind of quotes are needed around table names in raw SQL
73
+ originalTeardownOrder = deepCopy(teardownOrder);
74
+ };
75
+ let quoteStyle;
59
76
  const getQuoteStyle = async () => {
60
- const { getConfig: getPrismaConfig, getSchema } = require('@prisma/internals')
61
-
62
- // @NOTE prisma utils are available in cli lib/schemaHelpers
63
- // But avoid importing them, to prevent memory leaks in jest
64
- const datamodel = await getSchema(dbSchemaPath)
65
-
77
+ const { getConfig: getPrismaConfig, getSchema } = require("@prisma/internals");
78
+ const datamodel = await getSchema(dbSchemaPath);
66
79
  if (!quoteStyle) {
67
80
  const config = await getPrismaConfig({
68
- datamodel,
69
- })
70
-
81
+ datamodel
82
+ });
71
83
  switch (config.datasources?.[0]?.provider) {
72
- case 'mysql':
73
- quoteStyle = '`'
74
- break
84
+ case "mysql":
85
+ quoteStyle = "`";
86
+ break;
75
87
  default:
76
- quoteStyle = '"'
88
+ quoteStyle = '"';
77
89
  }
78
90
  }
79
-
80
- return quoteStyle
81
- }
82
-
83
- const getProjectDb = () => {
84
- const { db } = require(`${apiSrcPath}/lib/db`)
85
-
86
- return db
87
- }
88
-
89
- /**
90
- * Wraps "it" or "test", to seed and teardown the scenario after each test
91
- * This one passes scenario data to the test function
92
- */
93
- const buildScenario =
94
- (itFunc, testPath) =>
95
- (...args) => {
96
- let scenarioName, testName, testFunc
97
-
91
+ return quoteStyle;
92
+ };
93
+ const getProjectDb = async () => {
94
+ const { db } = require(`${apiSrcPath}/lib/db`);
95
+ return db;
96
+ };
97
+ function buildScenario(itFunc, testPath) {
98
+ const scenarioFunc = (...args) => {
99
+ let scenarioName, testName, testFunc;
98
100
  if (args.length === 3) {
99
- ;[scenarioName, testName, testFunc] = args
101
+ ;
102
+ [scenarioName, testName, testFunc] = args;
100
103
  } else if (args.length === 2) {
101
- scenarioName = DEFAULT_SCENARIO
102
- ;[testName, testFunc] = args
104
+ scenarioName = DEFAULT_SCENARIO;
105
+ [testName, testFunc] = args;
103
106
  } else {
104
- throw new Error('scenario() requires 2 or 3 arguments')
107
+ throw new Error("scenario() requires 2 or 3 arguments");
105
108
  }
106
-
107
109
  return itFunc(testName, async () => {
108
- let { scenario } = loadScenarios(testPath, scenarioName)
109
-
110
- const scenarioData = await seedScenario(scenario)
110
+ const { scenario } = await loadScenarios(testPath, scenarioName);
111
+ const scenarioData = await seedScenario(scenario);
111
112
  try {
112
- const result = await testFunc(scenarioData)
113
-
114
- return result
113
+ const result = await testFunc(scenarioData);
114
+ return result;
115
115
  } finally {
116
- // Make sure to cleanup, even if test fails
117
116
  if (wasDbUsed()) {
118
- await teardown()
117
+ await teardown();
119
118
  }
120
119
  }
121
- })
122
- }
123
-
124
- /**
125
- * This creates a describe() block that will seed the scenario ONCE before all tests in the block
126
- * Note that you need to use the getScenario() function to get the data.
127
- */
128
- const buildDescribeScenario =
129
- (describeFunc, testPath) =>
130
- (...args) => {
131
- let scenarioName, describeBlockName, describeBlock
132
-
120
+ });
121
+ };
122
+ return Object.assign(scenarioFunc, { only: scenarioFunc });
123
+ }
124
+ function buildDescribeScenario(describeFunc, testPath) {
125
+ const describeScenarioFunc = (...args) => {
126
+ let scenarioName, describeBlockName, describeBlock;
133
127
  if (args.length === 3) {
134
- ;[scenarioName, describeBlockName, describeBlock] = args
128
+ ;
129
+ [scenarioName, describeBlockName, describeBlock] = args;
135
130
  } else if (args.length === 2) {
136
- scenarioName = DEFAULT_SCENARIO
137
- ;[describeBlockName, describeBlock] = args
131
+ scenarioName = DEFAULT_SCENARIO;
132
+ [describeBlockName, describeBlock] = args;
138
133
  } else {
139
- throw new Error('describeScenario() requires 2 or 3 arguments')
134
+ throw new Error("describeScenario() requires 2 or 3 arguments");
140
135
  }
141
-
142
136
  return describeFunc(describeBlockName, () => {
143
- let scenarioData
137
+ let scenarioData;
144
138
  beforeAll(async () => {
145
- let { scenario } = loadScenarios(testPath, scenarioName)
146
- scenarioData = await seedScenario(scenario)
147
- })
148
-
139
+ const { scenario } = await loadScenarios(testPath, scenarioName);
140
+ scenarioData = await seedScenario(scenario);
141
+ });
149
142
  afterAll(async () => {
150
143
  if (wasDbUsed()) {
151
- await teardown()
144
+ await teardown();
152
145
  }
153
- })
154
-
155
- const getScenario = () => scenarioData
156
-
157
- describeBlock(getScenario)
158
- })
159
- }
160
-
146
+ });
147
+ const getScenario = () => scenarioData;
148
+ describeBlock(getScenario);
149
+ });
150
+ };
151
+ return Object.assign(describeScenarioFunc, { only: describeScenarioFunc });
152
+ }
161
153
  const teardown = async () => {
162
- const fs = require('fs')
163
-
164
- const quoteStyle = await getQuoteStyle()
165
-
166
- for (const modelName of teardownOrder) {
154
+ const quoteStyle2 = await getQuoteStyle();
155
+ for (let i = 0; i < teardownOrder.length; i++) {
156
+ const modelName = teardownOrder[i];
157
+ if (!modelName) {
158
+ continue;
159
+ }
167
160
  try {
168
- await getProjectDb().$executeRawUnsafe(
169
- `DELETE FROM ${quoteStyle}${modelName}${quoteStyle}`,
170
- )
161
+ const db = await getProjectDb();
162
+ await db.$executeRawUnsafe(
163
+ `DELETE FROM ${quoteStyle2}${modelName}${quoteStyle2}`
164
+ );
171
165
  } catch (e) {
172
- const match = e.message.match(/Code: `(\d+)`/)
166
+ const message = isErrorWithMessage(e) ? e.message : "";
167
+ const match = message.match(/Code: `(\d+)`/);
173
168
  if (match && FOREIGN_KEY_ERRORS.includes(parseInt(match[1]))) {
174
- const index = teardownOrder.indexOf(modelName)
175
- teardownOrder[index] = null
176
- teardownOrder.push(modelName)
169
+ teardownOrder.splice(i, 1);
170
+ teardownOrder.push(modelName);
171
+ i--;
177
172
  } else {
178
- throw e
173
+ throw e;
179
174
  }
180
175
  }
181
176
  }
182
-
183
- // remove nulls
184
- teardownOrder = teardownOrder.filter((val) => val)
185
-
186
- // if the order of delete changed, write out the cached file again
187
177
  if (!isIdenticalArray(teardownOrder, originalTeardownOrder)) {
188
- originalTeardownOrder = deepCopy(teardownOrder)
189
- fs.writeFileSync(TEARDOWN_CACHE_PATH, JSON.stringify(teardownOrder))
178
+ originalTeardownOrder = deepCopy(teardownOrder);
179
+ import_node_fs.default.writeFileSync(TEARDOWN_CACHE_PATH, JSON.stringify(teardownOrder));
190
180
  }
191
- }
192
-
181
+ };
193
182
  const seedScenario = async (scenario) => {
194
183
  if (scenario) {
195
- const scenarios = {}
184
+ const scenarios = {};
185
+ const db = await getProjectDb();
196
186
  for (const [model, namedFixtures] of Object.entries(scenario)) {
197
- scenarios[model] = {}
187
+ scenarios[model] = {};
198
188
  for (const [name, createArgs] of Object.entries(namedFixtures)) {
199
- if (typeof createArgs === 'function') {
200
- scenarios[model][name] = await getProjectDb()[model].create(
201
- createArgs(scenarios),
202
- )
189
+ if (typeof createArgs === "function") {
190
+ scenarios[model][name] = await db[model].create(createArgs(scenarios));
203
191
  } else {
204
- scenarios[model][name] =
205
- await getProjectDb()[model].create(createArgs)
192
+ scenarios[model][name] = await db[model].create(createArgs);
206
193
  }
207
194
  }
208
195
  }
209
- return scenarios
196
+ return scenarios;
210
197
  } else {
211
- return {}
198
+ return {};
212
199
  }
213
- }
214
-
215
- global.scenario = buildScenario(global.it, global.testPath)
216
- global.scenario.only = buildScenario(global.it.only, global.testPath)
200
+ };
201
+ global.scenario = buildScenario(global.it, global.testPath);
217
202
  global.describeScenario = buildDescribeScenario(
218
203
  global.describe,
219
- global.testPath,
220
- )
221
- global.describeScenario.only = buildDescribeScenario(
222
- global.describe.only,
223
- global.testPath,
224
- )
225
-
226
- /**
227
- *
228
- * All these hooks run in the VM/Context that the test runs in since we're using "setupAfterEnv".
229
- * There's a new context for each test-suite i.e. each test file
230
- *
231
- * Doing this means if the db isn't used in the current test context,
232
- * no need to do any of the teardown logic - allowing simple tests to run faster
233
- * At the same time, if the db is used, disconnecting it in this context prevents connection limit errors.
234
- * Just disconnecting db in jest-preset is not enough, because
235
- * the Prisma client is created in a different context.
236
- */
204
+ global.testPath
205
+ );
237
206
  const wasDbUsed = () => {
238
207
  try {
239
- const libDbPath = require.resolve(`${apiSrcPath}/lib/db`)
240
- return Object.keys(require.cache).some((module) => {
241
- return module === libDbPath
242
- })
243
- } catch (e) {
244
- // If db wasn't resolved, no point trying to perform db resets
245
- return false
208
+ const libDbPath = require.resolve(`${apiSrcPath}/lib/db`);
209
+ return Object.keys(require.cache).some((module2) => {
210
+ return module2 === libDbPath;
211
+ });
212
+ } catch {
213
+ return false;
246
214
  }
247
- }
248
-
249
- // Attempt to emulate the request context isolation behavior
250
- // This is a little more complicated than it would necessarily need to be
251
- // but we're following the same pattern as in `@cedarjs/context`
252
- const mockContextStore = new Map()
253
- const mockContext = new Proxy(
254
- {},
255
- {
256
- get: (_target, prop) => {
257
- // Handle toJSON() calls, i.e. JSON.stringify(context)
258
- if (prop === 'toJSON') {
259
- return () => mockContextStore.get('context')
260
- }
261
- return mockContextStore.get('context')[prop]
262
- },
263
- set: (_target, prop, value) => {
264
- const ctx = mockContextStore.get('context')
265
- ctx[prop] = value
266
- return true
267
- },
268
- },
269
- )
270
- jest.mock('@cedarjs/context', () => {
215
+ };
216
+ jest.mock("@cedarjs/context", () => {
271
217
  return {
272
218
  context: mockContext,
273
219
  setContext: (newContext) => {
274
- mockContextStore.set('context', newContext)
275
- },
276
- }
277
- })
220
+ mockContextStore.set("context", newContext);
221
+ }
222
+ };
223
+ });
278
224
  beforeEach(() => {
279
- mockContextStore.set('context', {})
280
- })
281
- global.mockCurrentUser = (currentUser) => {
282
- mockContextStore.set('context', { currentUser })
283
- }
284
-
225
+ mockContextStore.set("context", {});
226
+ });
285
227
  beforeAll(async () => {
286
228
  if (wasDbUsed()) {
287
- await configureTeardown()
229
+ await configureTeardown();
288
230
  }
289
- })
290
-
231
+ });
291
232
  afterAll(async () => {
292
233
  if (wasDbUsed()) {
293
- getProjectDb().$disconnect()
234
+ const db = await getProjectDb();
235
+ db.$disconnect();
294
236
  }
295
- })
296
-
297
- function loadScenarios(testPath, scenarioName) {
298
- const path = require('path')
299
- const testFileDir = path.parse(testPath)
300
- // e.g. ['comments', 'test'] or ['signup', 'state', 'machine', 'test']
301
- const testFileNameParts = testFileDir.name.split('.')
302
- const testFilePath = `${testFileDir.dir}/${testFileNameParts
303
- .slice(0, testFileNameParts.length - 1)
304
- .join('.')}.scenarios`
305
- let allScenarios, scenario
306
-
237
+ });
238
+ async function loadScenarios(testPath, scenarioName) {
239
+ const testFileDir = import_node_path.default.parse(testPath);
240
+ const testFileNameParts = testFileDir.name.split(".");
241
+ const testFilePath = `${testFileDir.dir}/${testFileNameParts.slice(0, testFileNameParts.length - 1).join(".")}.scenarios`;
242
+ let allScenarios;
243
+ let scenario;
307
244
  try {
308
- allScenarios = require(testFilePath)
245
+ allScenarios = require(testFilePath);
309
246
  } catch (e) {
310
- // ignore error if scenario file not found, otherwise re-throw
311
- if (e.code !== 'MODULE_NOT_FOUND') {
312
- throw e
247
+ if (isErrorWithCode(e) && e.code !== "MODULE_NOT_FOUND") {
248
+ throw new Error(`Failed to load scenario: ${e}`);
313
249
  }
314
250
  }
315
-
316
251
  if (allScenarios) {
317
252
  if (allScenarios[scenarioName]) {
318
- scenario = allScenarios[scenarioName]
253
+ scenario = allScenarios[scenarioName];
319
254
  } else {
320
255
  throw new Error(
321
- `UndefinedScenario: There is no scenario named "${scenarioName}" in ${testFilePath}.{js,ts}`,
322
- )
256
+ `UndefinedScenario: There is no scenario named "${scenarioName}" in ${testFilePath}.{js,ts}`
257
+ );
323
258
  }
324
259
  }
325
- return { scenario }
260
+ return { scenario };
261
+ }
262
+ function isErrorWithCode(error) {
263
+ return !!error && typeof error === "object" && "code" in error && typeof error.code === "string";
264
+ }
265
+ function isErrorWithMessage(error) {
266
+ return !!error && typeof error === "object" && "message" in error && typeof error.message === "string";
326
267
  }
@@ -0,0 +1,11 @@
1
+ import type { PluginObj, PluginPass } from '@babel/core';
2
+ import type * as t from '@babel/types';
3
+ interface PluginState extends PluginPass {
4
+ exportNames: string[];
5
+ hasDefaultExport: boolean;
6
+ }
7
+ export default function ({ types, }: {
8
+ types: typeof t;
9
+ }): PluginObj<PluginState>;
10
+ export {};
11
+ //# sourceMappingURL=babel-plugin-redwood-cell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"babel-plugin-redwood-cell.d.ts","sourceRoot":"","sources":["../../../../../src/config/jest/babelPlugins/babel-plugin-redwood-cell.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAA;AAqCtC,UAAU,WAAY,SAAQ,UAAU;IAKtC,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,gBAAgB,EAAE,OAAO,CAAA;CAC1B;AAED,MAAM,CAAC,OAAO,WAAW,EACvB,KAAK,GACN,EAAE;IACD,KAAK,EAAE,OAAO,CAAC,CAAA;CAChB,GAAG,SAAS,CAAC,WAAW,CAAC,CAkHzB"}