|
| 1 | +// Copyright (c) 2023 Developer Innovations, LLC |
| 2 | + |
| 3 | +import { loadConfig } from "./config"; |
| 4 | +import { cosmiconfigSync, Options } from "cosmiconfig"; |
| 5 | +import { |
| 6 | + setCosmiconfigSync, |
| 7 | + UnflakableConfigFile, |
| 8 | +} from "@unflakable/plugins-common"; |
| 9 | +import { IsFailureTestIndependentFn } from "./types"; |
| 10 | + |
| 11 | +const MOCK_SUITE_ID = "MOCK_SUITE_ID"; |
| 12 | +const SEARCH_FROM = "."; |
| 13 | + |
| 14 | +const throwUnimplemented = (): never => { |
| 15 | + throw new Error("unimplemented"); |
| 16 | +}; |
| 17 | + |
| 18 | +const setMockConfig = ( |
| 19 | + config: Partial< |
| 20 | + UnflakableConfigFile & { |
| 21 | + __unstableIsFailureTestIndependent: |
| 22 | + | string |
| 23 | + | RegExp |
| 24 | + | (string | RegExp)[] |
| 25 | + | IsFailureTestIndependentFn; |
| 26 | + } |
| 27 | + > |
| 28 | +): void => { |
| 29 | + setCosmiconfigSync( |
| 30 | + ( |
| 31 | + moduleName: string, |
| 32 | + options?: Options |
| 33 | + ): ReturnType<typeof cosmiconfigSync> => { |
| 34 | + expect(moduleName).toBe("unflakable"); |
| 35 | + expect(options?.searchPlaces).toContain("package.json"); |
| 36 | + expect(options?.searchPlaces).toContain("unflakable.json"); |
| 37 | + expect(options?.searchPlaces).toContain("unflakable.js"); |
| 38 | + expect(options?.searchPlaces).toContain("unflakable.yaml"); |
| 39 | + expect(options?.searchPlaces).toContain("unflakable.yml"); |
| 40 | + return { |
| 41 | + clearCaches: throwUnimplemented, |
| 42 | + clearLoadCache: throwUnimplemented, |
| 43 | + clearSearchCache: throwUnimplemented, |
| 44 | + load: throwUnimplemented, |
| 45 | + search: ( |
| 46 | + searchFrom?: string |
| 47 | + ): ReturnType<ReturnType<typeof cosmiconfigSync>["search"]> => { |
| 48 | + expect(searchFrom).toBe(SEARCH_FROM); |
| 49 | + return { |
| 50 | + config, |
| 51 | + filepath: "unflakable.js", |
| 52 | + isEmpty: false, |
| 53 | + }; |
| 54 | + }, |
| 55 | + }; |
| 56 | + } |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +describe("__unstableIsFailureTestIndependent", () => { |
| 61 | + it("should default to undefined", () => { |
| 62 | + setMockConfig({ testSuiteId: MOCK_SUITE_ID }); |
| 63 | + const config = loadConfig(SEARCH_FROM); |
| 64 | + expect(config.testSuiteId).toBe(MOCK_SUITE_ID); |
| 65 | + expect(config.isFailureTestIndependent).toBeUndefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should accept a string regex", () => { |
| 69 | + setMockConfig({ |
| 70 | + testSuiteId: MOCK_SUITE_ID, |
| 71 | + __unstableIsFailureTestIndependent: ".*", |
| 72 | + }); |
| 73 | + const config = loadConfig(SEARCH_FROM); |
| 74 | + expect(config.testSuiteId).toBe(MOCK_SUITE_ID); |
| 75 | + expect(Array.isArray(config.isFailureTestIndependent)).toBe(true); |
| 76 | + expect(config.isFailureTestIndependent).toHaveLength(1); |
| 77 | + expect((config.isFailureTestIndependent as RegExp[])[0]).toBeInstanceOf( |
| 78 | + RegExp |
| 79 | + ); |
| 80 | + expect((config.isFailureTestIndependent as RegExp[])[0].source).toBe(".*"); |
| 81 | + expect((config.isFailureTestIndependent as RegExp[])[0].flags).toBe(""); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should accept a RegExp object", () => { |
| 85 | + setMockConfig({ |
| 86 | + testSuiteId: MOCK_SUITE_ID, |
| 87 | + __unstableIsFailureTestIndependent: /.*/gs, |
| 88 | + }); |
| 89 | + const config = loadConfig(SEARCH_FROM); |
| 90 | + expect(config.testSuiteId).toBe(MOCK_SUITE_ID); |
| 91 | + expect(Array.isArray(config.isFailureTestIndependent)).toBe(true); |
| 92 | + expect(config.isFailureTestIndependent).toHaveLength(1); |
| 93 | + expect((config.isFailureTestIndependent as RegExp[])[0]).toBeInstanceOf( |
| 94 | + RegExp |
| 95 | + ); |
| 96 | + expect((config.isFailureTestIndependent as RegExp[])[0].source).toBe(".*"); |
| 97 | + expect((config.isFailureTestIndependent as RegExp[])[0].flags).toBe("gs"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should accept an array of strings/RegExps object", () => { |
| 101 | + setMockConfig({ |
| 102 | + testSuiteId: MOCK_SUITE_ID, |
| 103 | + __unstableIsFailureTestIndependent: [/foo/s, /bar/g, "baz", ".*"], |
| 104 | + }); |
| 105 | + const config = loadConfig(SEARCH_FROM); |
| 106 | + expect(config.testSuiteId).toBe(MOCK_SUITE_ID); |
| 107 | + expect(Array.isArray(config.isFailureTestIndependent)).toBe(true); |
| 108 | + expect(config.isFailureTestIndependent).toHaveLength(4); |
| 109 | + expect((config.isFailureTestIndependent as RegExp[])[0]).toBeInstanceOf( |
| 110 | + RegExp |
| 111 | + ); |
| 112 | + expect((config.isFailureTestIndependent as RegExp[])[0].source).toBe("foo"); |
| 113 | + expect((config.isFailureTestIndependent as RegExp[])[0].flags).toBe("s"); |
| 114 | + expect((config.isFailureTestIndependent as RegExp[])[1]).toBeInstanceOf( |
| 115 | + RegExp |
| 116 | + ); |
| 117 | + expect((config.isFailureTestIndependent as RegExp[])[1].source).toBe("bar"); |
| 118 | + expect((config.isFailureTestIndependent as RegExp[])[1].flags).toBe("g"); |
| 119 | + expect((config.isFailureTestIndependent as RegExp[])[2]).toBeInstanceOf( |
| 120 | + RegExp |
| 121 | + ); |
| 122 | + expect((config.isFailureTestIndependent as RegExp[])[2].source).toBe("baz"); |
| 123 | + expect((config.isFailureTestIndependent as RegExp[])[2].flags).toBe(""); |
| 124 | + expect((config.isFailureTestIndependent as RegExp[])[3]).toBeInstanceOf( |
| 125 | + RegExp |
| 126 | + ); |
| 127 | + expect((config.isFailureTestIndependent as RegExp[])[3].source).toBe(".*"); |
| 128 | + expect((config.isFailureTestIndependent as RegExp[])[3].flags).toBe(""); |
| 129 | + }); |
| 130 | +}); |
0 commit comments