Allow fixture tests to be `only` or `skip`
This commit introduces a small update to our fixture tests to allow certain jest `test` modifiers to be added to fixture tests via a leading prefix in the fixture name. For example, `only.my-fixture.js` is the equivalent of writing `test.only("my-fixture")`.
Lauren Tan committed
Nov 3, 2022 at 18:47 UTC
a9419099b4b5bbd130162d15ed59cba03c7caccc
1 file changed
+41
-19
compiler/forget/src/__tests__/test-utils/generateTestsFromFixtures.ts
+41
-19
@@ -67,10 +67,27 @@ export default function generateTestsFromFixtures(
67
});
68
69
describe("fixtures", () => {
70
- test.each(Array.from(fixtures.values()))(
71
- "$basename",
72
- ({ basename, input: inputFile, output: outputFile }) => {
73
- let receivedOutput;
70
+ for (const {
71
+ basename,
72
+ input: inputFile,
73
+ output: outputFile,
74
+ } of Array.from(fixtures.values())) {
75
+ let receivedOutput;
76
+ let testCommand;
77
+
78
+ switch (basename.split(".")[0]) {
79
+ case "only":
80
+ testCommand = test.only;
81
+ break;
82
+ case "skip":
83
+ testCommand = test.skip;
84
+ break;
85
+ default:
86
+ testCommand = test;
87
+ break;
88
+ }
89
+
90
+ testCommand(basename, () => {
91
if (inputFile != null) {
92
const input = fs.readFileSync(inputFile, "utf8");
93
receivedOutput = transform(input, basename);
@@ -85,23 +102,25 @@ export default function generateTestsFromFixtures(
102
// Determine whether the snapshot is in update mode or only creating snapshots for new inputs
103
// to update the .expect file in parallel with updating the snapshot itself.
104
const snapshotUpdateMode = determineSnapshotMode();
88
- const outputExists = fs.existsSync(outputFile);
89
- if (
90
- snapshotUpdateMode === "all" ||
91
- (snapshotUpdateMode === "new" && !outputExists)
92
- ) {
93
- if (inputFile != null) {
94
- fs.writeFileSync(outputFile, receivedOutput, "utf8");
105
+ if (outputFile != null) {
106
+ const outputExists = fs.existsSync(outputFile);
107
+ if (
108
+ snapshotUpdateMode === "all" ||
109
+ (snapshotUpdateMode === "new" && !outputExists)
110
+ ) {
111
+ if (inputFile != null) {
112
+ fs.writeFileSync(outputFile, receivedOutput, "utf8");
113
+ } else {
114
+ fs.unlinkSync(outputFile);
115
+ }
116
} else {
96
- fs.unlinkSync(outputFile);
117
+ // As a sanity check, make sure that the current output matches the .expect file
118
+ const actualOutput = fs.readFileSync(outputFile, "utf8");
119
+ expect(receivedOutput).toEqual(actualOutput);
120
}
98
- } else if (outputFile != null) {
99
- // As a sanity check, make sure that the current output matches the .expect file
100
- const actualOutput = fs.readFileSync(outputFile, "utf8");
101
- expect(receivedOutput).toEqual(actualOutput);
121
}
103
- }
104
- );
122
+ });
123
+ }
124
});
125
});
126
}
@@ -132,7 +151,10 @@ function determineSnapshotMode() {
151
}
152
153
function matchInputOutputFixtures(files: string[], fixturesPath: string) {
135
- const fixtures = new Map();
154
+ const fixtures: Map<
155
+ string,
156
+ { basename: string; input: string | null; output: string | null }
157
+ > = new Map();
158
for (const file of files) {
159
const isOutput = file.endsWith(EXPECT_SUFFIX);
160
const basename = path.basename(