| 1 | "use strict"; |
| 2 | |
| 3 | const path = require("node:path"); |
| 4 | const { |
| 5 | touchFile, |
| 6 | runMochaAsync, |
| 7 | runMochaWatchAsync, |
| 8 | copyFixture, |
| 9 | DEFAULT_FIXTURE, |
| 10 | resolveFixturePath, |
| 11 | createTempDir, |
| 12 | } = require("../helpers"); |
| 13 | |
| 14 | describe("global setup/teardown", function () { |
| 15 | describe("when mocha run in serial mode", function () { |
| 16 | it("should execute global setup and teardown", async function () { |
| 17 | return expect( |
| 18 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 19 | "--require", |
| 20 | resolveFixturePath("plugins/global-fixtures/global-setup-teardown"), |
| 21 | ]), |
| 22 | "when fulfilled", |
| 23 | "to have passed", |
| 24 | ); |
| 25 | }); |
| 26 | |
| 27 | describe("when only global teardown is supplied", function () { |
| 28 | it("should run global teardown", async function () { |
| 29 | return expect( |
| 30 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 31 | "--require", |
| 32 | resolveFixturePath("plugins/global-fixtures/global-teardown"), |
| 33 | ]), |
| 34 | "when fulfilled", |
| 35 | "to contain once", |
| 36 | /teardown schmeardown/, |
| 37 | ); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe("when only global setup is supplied", function () { |
| 42 | it("should run global setup", async function () { |
| 43 | return expect( |
| 44 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 45 | "--require", |
| 46 | resolveFixturePath("plugins/global-fixtures/global-setup"), |
| 47 | ]), |
| 48 | "when fulfilled", |
| 49 | "to contain once", |
| 50 | /setup schmetup/, |
| 51 | ); |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | it("should share context", async function () { |
| 56 | return expect( |
| 57 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 58 | "--require", |
| 59 | resolveFixturePath("plugins/global-fixtures/global-setup-teardown"), |
| 60 | ]), |
| 61 | "when fulfilled", |
| 62 | "to contain once", |
| 63 | /setup: this\.foo = bar[\s\S]+teardown: this\.foo = bar/, |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | describe("when supplied multiple functions", function () { |
| 68 | it("should execute them sequentially", async function () { |
| 69 | return expect( |
| 70 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 71 | "--require", |
| 72 | resolveFixturePath( |
| 73 | "plugins/global-fixtures/global-setup-teardown-multiple", |
| 74 | ), |
| 75 | ]), |
| 76 | "when fulfilled", |
| 77 | "to contain once", |
| 78 | /teardown: this.foo = 3/, |
| 79 | ); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | describe("when run in watch mode", function () { |
| 84 | let tempDir; |
| 85 | let testFile; |
| 86 | let removeTempDir; |
| 87 | |
| 88 | beforeEach(async function () { |
| 89 | const tempInfo = await createTempDir(); |
| 90 | tempDir = tempInfo.dirpath; |
| 91 | removeTempDir = tempInfo.removeTempDir; |
| 92 | testFile = path.join(tempDir, "test.js"); |
| 93 | copyFixture(DEFAULT_FIXTURE, testFile); |
| 94 | }); |
| 95 | |
| 96 | afterEach(async function () { |
| 97 | if (removeTempDir) { |
| 98 | return removeTempDir(); |
| 99 | } |
| 100 | }); |
| 101 | |
| 102 | it("should execute global setup and teardown", async function () { |
| 103 | return expect( |
| 104 | runMochaWatchAsync( |
| 105 | [ |
| 106 | "--require", |
| 107 | resolveFixturePath( |
| 108 | "plugins/global-fixtures/global-setup-teardown", |
| 109 | ), |
| 110 | testFile, |
| 111 | ], |
| 112 | tempDir, |
| 113 | () => { |
| 114 | touchFile(testFile); |
| 115 | }, |
| 116 | ), |
| 117 | "when fulfilled", |
| 118 | "to have passed", |
| 119 | ); |
| 120 | }); |
| 121 | |
| 122 | describe("when only global teardown is supplied", function () { |
| 123 | it("should run global teardown", async function () { |
| 124 | return expect( |
| 125 | runMochaWatchAsync( |
| 126 | [ |
| 127 | "--require", |
| 128 | resolveFixturePath("plugins/global-fixtures/global-teardown"), |
| 129 | testFile, |
| 130 | ], |
| 131 | tempDir, |
| 132 | () => { |
| 133 | touchFile(testFile); |
| 134 | }, |
| 135 | ), |
| 136 | "when fulfilled", |
| 137 | "to contain once", |
| 138 | /teardown schmeardown/, |
| 139 | ); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("when only global setup is supplied", function () { |
| 144 | it("should run global setup", async function () { |
| 145 | return expect( |
| 146 | runMochaWatchAsync( |
| 147 | [ |
| 148 | "--require", |
| 149 | resolveFixturePath("plugins/global-fixtures/global-setup"), |
| 150 | testFile, |
| 151 | ], |
| 152 | tempDir, |
| 153 | () => { |
| 154 | touchFile(testFile); |
| 155 | }, |
| 156 | ), |
| 157 | "when fulfilled", |
| 158 | "to contain once", |
| 159 | /setup schmetup/, |
| 160 | ); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | it("should not re-execute the global fixtures", async function () { |
| 165 | return expect( |
| 166 | runMochaWatchAsync( |
| 167 | [ |
| 168 | "--require", |
| 169 | resolveFixturePath( |
| 170 | "plugins/global-fixtures/global-setup-teardown-multiple", |
| 171 | ), |
| 172 | testFile, |
| 173 | ], |
| 174 | tempDir, |
| 175 | () => { |
| 176 | touchFile(testFile); |
| 177 | }, |
| 178 | ), |
| 179 | "when fulfilled", |
| 180 | "to contain once", |
| 181 | /teardown: this.foo = 3/, |
| 182 | ); |
| 183 | }); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe("when mocha run in parallel mode", function () { |
| 188 | it("should execute global setup and teardown", async function () { |
| 189 | return expect( |
| 190 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 191 | "--parallel", |
| 192 | "--require", |
| 193 | resolveFixturePath("plugins/global-fixtures/global-setup-teardown"), |
| 194 | ]), |
| 195 | "when fulfilled", |
| 196 | "to have passed", |
| 197 | ); |
| 198 | }); |
| 199 | |
| 200 | it("should share context", async function () { |
| 201 | return expect( |
| 202 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 203 | "--parallel", |
| 204 | "--require", |
| 205 | resolveFixturePath("plugins/global-fixtures/global-setup-teardown"), |
| 206 | ]), |
| 207 | "when fulfilled", |
| 208 | "to contain once", |
| 209 | /setup: this.foo = bar/, |
| 210 | ).and("when fulfilled", "to contain once", /teardown: this.foo = bar/); |
| 211 | }); |
| 212 | |
| 213 | describe("when supplied multiple functions", function () { |
| 214 | it("should execute them sequentially", async function () { |
| 215 | return expect( |
| 216 | runMochaAsync(DEFAULT_FIXTURE, [ |
| 217 | "--parallel", |
| 218 | "--require", |
| 219 | resolveFixturePath( |
| 220 | "plugins/global-fixtures/global-setup-teardown-multiple", |
| 221 | ), |
| 222 | ]), |
| 223 | "when fulfilled", |
| 224 | "to contain once", |
| 225 | /teardown: this.foo = 3/, |
| 226 | ); |
| 227 | }); |
| 228 | }); |
| 229 | |
| 230 | describe("when run in watch mode", function () { |
| 231 | let tempDir; |
| 232 | let testFile; |
| 233 | let removeTempDir; |
| 234 | |
| 235 | beforeEach(async function () { |
| 236 | const tempInfo = await createTempDir(); |
| 237 | tempDir = tempInfo.dirpath; |
| 238 | removeTempDir = tempInfo.removeTempDir; |
| 239 | testFile = path.join(tempDir, "test.js"); |
| 240 | copyFixture(DEFAULT_FIXTURE, testFile); |
| 241 | }); |
| 242 | |
| 243 | afterEach(async function () { |
| 244 | if (removeTempDir) { |
| 245 | return removeTempDir(); |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | it("should execute global setup and teardown", async function () { |
| 250 | return expect( |
| 251 | runMochaWatchAsync( |
| 252 | [ |
| 253 | "--parallel", |
| 254 | "--require", |
| 255 | resolveFixturePath( |
| 256 | "plugins/global-fixtures/global-setup-teardown", |
| 257 | ), |
| 258 | testFile, |
| 259 | ], |
| 260 | tempDir, |
| 261 | () => { |
| 262 | touchFile(testFile); |
| 263 | }, |
| 264 | ), |
| 265 | "when fulfilled", |
| 266 | "to have passed", |
| 267 | ); |
| 268 | }); |
| 269 | |
| 270 | it("should not re-execute the global fixtures", async function () { |
| 271 | return expect( |
| 272 | runMochaWatchAsync( |
| 273 | [ |
| 274 | "--parallel", |
| 275 | "--require", |
| 276 | resolveFixturePath( |
| 277 | "plugins/global-fixtures/global-setup-teardown-multiple", |
| 278 | ), |
| 279 | testFile, |
| 280 | ], |
| 281 | tempDir, |
| 282 | () => { |
| 283 | touchFile(testFile); |
| 284 | }, |
| 285 | ), |
| 286 | "when fulfilled", |
| 287 | "to contain once", |
| 288 | /teardown: this.foo = 3/, |
| 289 | ); |
| 290 | }); |
| 291 | }); |
| 292 | }); |
| 293 | }); |