|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { getIgnoreMatchers } = require("../../lib/getGlobMatchers"); |
| 4 | + |
| 5 | +describe("getIgnoreMatchers", () => { |
| 6 | + it("should return an array of matchers for glob strings", () => { |
| 7 | + const watchOptions = { |
| 8 | + cwd: process.cwd(), |
| 9 | + ignored: ["src/*.js", "tests/*.spec.js"], |
| 10 | + }; |
| 11 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 12 | + |
| 13 | + expect(matchers).toHaveLength(1); |
| 14 | + expect(typeof matchers[0]).toBe("function"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return the original value for non-glob strings", () => { |
| 18 | + const watchOptions = { cwd: process.cwd(), ignored: "src/file.txt" }; |
| 19 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 20 | + |
| 21 | + expect(matchers).toHaveLength(1); |
| 22 | + expect(matchers[0]).toBe("src/file.txt"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should return empty array if ignored is not defined", () => { |
| 26 | + const watchOptions = { cwd: process.cwd() }; |
| 27 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 28 | + |
| 29 | + expect(matchers).toEqual([]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return an array that includes the passed matcher function", () => { |
| 33 | + const watchOptions = { cwd: process.cwd() }; |
| 34 | + const ignoreFunction = () => true; |
| 35 | + const matchers = getIgnoreMatchers(watchOptions, ignoreFunction); |
| 36 | + |
| 37 | + expect(matchers).toHaveLength(1); |
| 38 | + expect(matchers[0]).toBe(ignoreFunction); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return all original value and only replace all globs with one function", () => { |
| 42 | + const ignoreFunction = () => true; |
| 43 | + const regex = /src\/.*\.js/; |
| 44 | + const watchOptions = { |
| 45 | + cwd: process.cwd(), |
| 46 | + ignored: [ |
| 47 | + "src/*.js", |
| 48 | + "src/file.txt", |
| 49 | + "src/**/*.js", |
| 50 | + ignoreFunction, |
| 51 | + regex, |
| 52 | + ], |
| 53 | + }; |
| 54 | + |
| 55 | + const matchers = getIgnoreMatchers(watchOptions, ignoreFunction); |
| 56 | + |
| 57 | + expect(matchers).toHaveLength(5); |
| 58 | + expect(matchers[0]).toBe("src/file.txt"); |
| 59 | + expect(matchers[1]).toBe(ignoreFunction); |
| 60 | + expect(matchers[2]).toBe(regex); |
| 61 | + expect(matchers[3]).toBe(ignoreFunction); |
| 62 | + expect(typeof matchers[4]).toBe("function"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should work with complicated glob", () => { |
| 66 | + const watchOptions = { |
| 67 | + cwd: process.cwd(), |
| 68 | + ignored: ["src/**/components/*.js"], |
| 69 | + }; |
| 70 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 71 | + |
| 72 | + expect(matchers).toHaveLength(1); |
| 73 | + expect(typeof matchers[0]).toBe("function"); |
| 74 | + |
| 75 | + const filePath = "src/components/file.txt"; |
| 76 | + expect(matchers[0](filePath)).toBe(false); |
| 77 | + |
| 78 | + const jsFilePath = "src/components/file.js"; |
| 79 | + expect(matchers[0](jsFilePath)).toBe(true); |
| 80 | + |
| 81 | + const jsFilePath2 = "src/other/components/file.js"; |
| 82 | + expect(matchers[0](jsFilePath2)).toBe(true); |
| 83 | + |
| 84 | + const nestedJsFilePath = "src/components/nested/file.js"; |
| 85 | + expect(matchers[0](nestedJsFilePath)).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should work with negated glob", () => { |
| 89 | + const watchOptions = { |
| 90 | + cwd: process.cwd(), |
| 91 | + ignored: ["src/**/components/!(*.spec).js"], |
| 92 | + }; |
| 93 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 94 | + |
| 95 | + expect(matchers).toHaveLength(1); |
| 96 | + expect(typeof matchers[0]).toBe("function"); |
| 97 | + |
| 98 | + const filePath = "src/components/file.txt"; |
| 99 | + expect(matchers[0](filePath)).toBe(false); |
| 100 | + |
| 101 | + const jsFilePath = "src/components/file.js"; |
| 102 | + expect(matchers[0](jsFilePath)).toBe(true); |
| 103 | + |
| 104 | + const specJsFilePath = "src/components/file.spec.js"; |
| 105 | + expect(matchers[0](specJsFilePath)).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should work with directory glob", () => { |
| 109 | + const watchOptions = { cwd: process.cwd(), ignored: ["src/**"] }; |
| 110 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 111 | + |
| 112 | + expect(matchers).toHaveLength(1); |
| 113 | + expect(typeof matchers[0]).toBe("function"); |
| 114 | + |
| 115 | + const filePath = "src/file.txt"; |
| 116 | + expect(matchers[0](filePath)).toBe(true); |
| 117 | + |
| 118 | + const dirPath = "src/subdir"; |
| 119 | + expect(matchers[0](dirPath)).toBe(true); |
| 120 | + |
| 121 | + const nestedFilePath = "src/subdir/nested/file.txt"; |
| 122 | + expect(matchers[0](nestedFilePath)).toBe(true); |
| 123 | + |
| 124 | + const wrongPath = "foo/bar"; |
| 125 | + expect(matchers[0](wrongPath)).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should work with directory glob and file extension", () => { |
| 129 | + const watchOptions = { cwd: process.cwd(), ignored: ["src/**/*.{js,ts}"] }; |
| 130 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 131 | + |
| 132 | + expect(matchers).toHaveLength(1); |
| 133 | + expect(typeof matchers[0]).toBe("function"); |
| 134 | + |
| 135 | + const jsFilePath = "src/file.js"; |
| 136 | + expect(matchers[0](jsFilePath)).toBe(true); |
| 137 | + |
| 138 | + const tsFilePath = "src/file.ts"; |
| 139 | + expect(matchers[0](tsFilePath)).toBe(true); |
| 140 | + |
| 141 | + const txtFilePath = "src/file.txt"; |
| 142 | + expect(matchers[0](txtFilePath)).toBe(false); |
| 143 | + |
| 144 | + const nestedJsFilePath = "src/subdir/nested/file.js"; |
| 145 | + expect(matchers[0](nestedJsFilePath)).toBe(true); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should return the input as array when globbing is disabled", () => { |
| 149 | + const watchOptions = { |
| 150 | + cwd: process.cwd(), |
| 151 | + disableGlobbing: true, |
| 152 | + ignored: "src/**/*.{js,ts}", |
| 153 | + }; |
| 154 | + const matchers = getIgnoreMatchers(watchOptions, null); |
| 155 | + |
| 156 | + expect(matchers).toHaveLength(1); |
| 157 | + expect(matchers[0]).toBe("src/**/*.{js,ts}"); |
| 158 | + }); |
| 159 | +}); |
0 commit comments