|
| 1 | +import { workspaceRoot } from '@nrwl/devkit'; |
| 2 | +import { qwikNxVite, QwikNxVitePluginOptions } from './qwik-nx-vite.plugin'; |
| 3 | + |
| 4 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 5 | +const fileUtils = require('nx/src/project-graph/file-utils'); |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +const workspaceConfig1 = { |
| 10 | + projects: { |
| 11 | + 'tmp-test-app-a': { |
| 12 | + root: 'apps/test-app-a', |
| 13 | + name: 'tmp-test-app-a', |
| 14 | + projectType: 'application', |
| 15 | + sourceRoot: 'apps/test-app-a/src', |
| 16 | + prefix: 'tmp', |
| 17 | + tags: ['tag1', 'tag2'], |
| 18 | + }, |
| 19 | + 'tmp-test-lib-a': { |
| 20 | + root: 'libs/test-lib-a', |
| 21 | + name: 'tmp-test-lib-a', |
| 22 | + projectType: 'library', |
| 23 | + sourceRoot: 'libs/test-lib-a/src', |
| 24 | + prefix: 'tmp', |
| 25 | + tags: ['tag2'], |
| 26 | + }, |
| 27 | + 'tmp-test-lib-b': { |
| 28 | + root: 'libs/test-lib-b', |
| 29 | + name: 'tmp-test-lib-b', |
| 30 | + projectType: 'library', |
| 31 | + sourceRoot: 'libs/test-lib-b/src', |
| 32 | + prefix: 'tmp', |
| 33 | + tags: ['tag2', 'tag3'], |
| 34 | + }, |
| 35 | + 'tmp-test-lib-c-nested-1': { |
| 36 | + root: 'libs/test-lib-c/nested', |
| 37 | + name: 'tmp-test-lib-c-nested-1', |
| 38 | + projectType: 'library', |
| 39 | + sourceRoot: 'libs/test-lib-c/nested-1/src', |
| 40 | + prefix: 'tmp', |
| 41 | + tags: ['tag4'], |
| 42 | + }, |
| 43 | + 'tmp-test-lib-c-nested-2': { |
| 44 | + root: 'libs/test-lib-c/nested', |
| 45 | + name: 'tmp-test-lib-c-nested-2', |
| 46 | + projectType: 'library', |
| 47 | + sourceRoot: 'libs/test-lib-c/nested-2/src', |
| 48 | + prefix: 'tmp', |
| 49 | + tags: ['tag1', 'tag2', 'tag3'], |
| 50 | + }, |
| 51 | + 'tmp-other-test-lib-a': { |
| 52 | + root: 'libs/other/test-lib-a', |
| 53 | + name: 'tmp-other-test-lib-a', |
| 54 | + projectType: 'library', |
| 55 | + sourceRoot: 'libs/other/test-lib-a/src', |
| 56 | + prefix: 'tmp', |
| 57 | + tags: [], |
| 58 | + }, |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +const tsConfigString1 = JSON.stringify({ |
| 63 | + compilerOptions: { |
| 64 | + paths: { |
| 65 | + '@tmp/test-lib-a': 'libs/test-lib-a/src/index.ts', |
| 66 | + '@tmp/test-lib-b': 'libs/test-lib-b/src/index.ts', |
| 67 | + '@tmp/test-lib-c/nested-1': 'libs/test-lib-c/nested-1/src/index.ts', |
| 68 | + '@tmp/test-lib-c/nested-2': 'libs/test-lib-c/nested-2/src/index.ts', |
| 69 | + '@tmp/other/test-lib-a/nested-2': 'libs/other/test-lib-a/src/index.ts', |
| 70 | + }, |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +describe('qwik-nx-vite plugin', () => { |
| 75 | + jest |
| 76 | + .spyOn(fileUtils, 'readWorkspaceConfig') |
| 77 | + .mockReturnValue(workspaceConfig1); |
| 78 | + jest.spyOn(fs, 'readFileSync').mockReturnValue(tsConfigString1); |
| 79 | + |
| 80 | + const getDecoratedPaths = async (options?: QwikNxVitePluginOptions) => { |
| 81 | + const plugin = qwikNxVite(options); |
| 82 | + const vendorRoots = []; |
| 83 | + const qwikViteMock = { |
| 84 | + name: 'vite-plugin-qwik', |
| 85 | + api: { |
| 86 | + getOptions: () => ({ vendorRoots }), |
| 87 | + }, |
| 88 | + }; |
| 89 | + await (plugin.configResolved as any)({ plugins: [qwikViteMock] }); |
| 90 | + return vendorRoots; |
| 91 | + }; |
| 92 | + |
| 93 | + it('Without filters', async () => { |
| 94 | + const paths = await getDecoratedPaths(); |
| 95 | + |
| 96 | + expect(paths).toEqual([ |
| 97 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 98 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 99 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 100 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 101 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Name filter', () => { |
| 106 | + describe('As string', () => { |
| 107 | + it('Exclude', async () => { |
| 108 | + const paths = await getDecoratedPaths({ |
| 109 | + excludeProjects: { |
| 110 | + name: ['tmp-test-lib-b', 'tmp-test-lib-c-nested-2'], |
| 111 | + }, |
| 112 | + }); |
| 113 | + expect(paths).toEqual([ |
| 114 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 115 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 116 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 117 | + ]); |
| 118 | + }); |
| 119 | + it('Include', async () => { |
| 120 | + const paths = await getDecoratedPaths({ |
| 121 | + includeProjects: { |
| 122 | + name: ['tmp-test-lib-b', 'tmp-test-lib-c-nested-2'], |
| 123 | + }, |
| 124 | + }); |
| 125 | + expect(paths).toEqual([ |
| 126 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 127 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 128 | + ]); |
| 129 | + }); |
| 130 | + it('Both', async () => { |
| 131 | + const paths = await getDecoratedPaths({ |
| 132 | + includeProjects: { name: ['tmp-test-lib-c-nested-2'] }, |
| 133 | + excludeProjects: { name: ['tmp-test-lib-b'] }, |
| 134 | + }); |
| 135 | + expect(paths).toEqual([ |
| 136 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 137 | + ]); |
| 138 | + }); |
| 139 | + }); |
| 140 | + describe('As regexp', () => { |
| 141 | + it('Exclude', async () => { |
| 142 | + const paths = await getDecoratedPaths({ |
| 143 | + excludeProjects: { name: /lib-a/ }, |
| 144 | + }); |
| 145 | + expect(paths).toEqual([ |
| 146 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 147 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 148 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 149 | + ]); |
| 150 | + }); |
| 151 | + it('Exclude - ends with', async () => { |
| 152 | + const paths = await getDecoratedPaths({ |
| 153 | + excludeProjects: { name: /tmp-test-lib-\w$/ }, |
| 154 | + }); |
| 155 | + expect(paths).toEqual([ |
| 156 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 157 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 158 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + it('Exclude - wrong value', async () => { |
| 162 | + const paths = await getDecoratedPaths({ |
| 163 | + excludeProjects: { name: /test-lib$/ }, |
| 164 | + }); |
| 165 | + expect(paths).toEqual([ |
| 166 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 167 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 168 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 169 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 170 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 171 | + ]); |
| 172 | + }); |
| 173 | + it('Include', async () => { |
| 174 | + const paths = await getDecoratedPaths({ |
| 175 | + includeProjects: { name: /nested/ }, |
| 176 | + }); |
| 177 | + expect(paths).toEqual([ |
| 178 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 179 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 180 | + ]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('Include - with "global" flag', async () => { |
| 184 | + const paths = await getDecoratedPaths({ |
| 185 | + includeProjects: { name: /nested/g }, |
| 186 | + }); |
| 187 | + expect(paths).toEqual([ |
| 188 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 189 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 190 | + ]); |
| 191 | + }); |
| 192 | + |
| 193 | + it('Both', async () => { |
| 194 | + const paths = await getDecoratedPaths({ |
| 195 | + includeProjects: { name: /nested/ }, |
| 196 | + excludeProjects: { name: /nested-2/ }, |
| 197 | + }); |
| 198 | + expect(paths).toEqual([ |
| 199 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 200 | + ]); |
| 201 | + }); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe('Path filter', () => { |
| 206 | + it('Exclude', async () => { |
| 207 | + const paths = await getDecoratedPaths({ |
| 208 | + excludeProjects: { path: /other\/test/ }, |
| 209 | + }); |
| 210 | + expect(paths).toEqual([ |
| 211 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 212 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 213 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 214 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 215 | + ]); |
| 216 | + }); |
| 217 | + it('Include', async () => { |
| 218 | + const paths = await getDecoratedPaths({ |
| 219 | + includeProjects: { path: /nested/ }, |
| 220 | + }); |
| 221 | + expect(paths).toEqual([ |
| 222 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 223 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 224 | + ]); |
| 225 | + }); |
| 226 | + |
| 227 | + it('Include - with "global" flag', async () => { |
| 228 | + const paths = await getDecoratedPaths({ |
| 229 | + includeProjects: { path: /nested/g }, |
| 230 | + }); |
| 231 | + expect(paths).toEqual([ |
| 232 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 233 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 234 | + ]); |
| 235 | + }); |
| 236 | + |
| 237 | + it('Both', async () => { |
| 238 | + const paths = await getDecoratedPaths({ |
| 239 | + includeProjects: { path: /lib-a/ }, |
| 240 | + excludeProjects: { path: /other/ }, |
| 241 | + }); |
| 242 | + expect(paths).toEqual([`${workspaceRoot}/libs/test-lib-a/src`]); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + describe('Tags filter', () => { |
| 247 | + it('Exclude', async () => { |
| 248 | + const paths = await getDecoratedPaths({ |
| 249 | + excludeProjects: { tags: ['tag1'] }, |
| 250 | + }); |
| 251 | + expect(paths).toEqual([ |
| 252 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 253 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 254 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 255 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 256 | + ]); |
| 257 | + }); |
| 258 | + it('Exclude multiple', async () => { |
| 259 | + const paths = await getDecoratedPaths({ |
| 260 | + excludeProjects: { tags: ['tag1', 'tag3'] }, |
| 261 | + }); |
| 262 | + expect(paths).toEqual([ |
| 263 | + `${workspaceRoot}/libs/test-lib-a/src`, |
| 264 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 265 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 266 | + ]); |
| 267 | + }); |
| 268 | + it('Include', async () => { |
| 269 | + const paths = await getDecoratedPaths({ |
| 270 | + includeProjects: { tags: ['tag3'] }, |
| 271 | + }); |
| 272 | + expect(paths).toEqual([ |
| 273 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 274 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 275 | + ]); |
| 276 | + }); |
| 277 | + it('Include multiple', async () => { |
| 278 | + const paths = await getDecoratedPaths({ |
| 279 | + includeProjects: { tags: ['tag1', 'tag3'] }, |
| 280 | + }); |
| 281 | + expect(paths).toEqual([`${workspaceRoot}/libs/test-lib-c/nested-2/src`]); |
| 282 | + }); |
| 283 | + }); |
| 284 | + describe('Custom filter', () => { |
| 285 | + it('Exclude', async () => { |
| 286 | + const paths = await getDecoratedPaths({ |
| 287 | + excludeProjects: { customFilter: (p) => p.name === 'tmp-test-lib-a' }, |
| 288 | + }); |
| 289 | + expect(paths).toEqual([ |
| 290 | + `${workspaceRoot}/libs/test-lib-b/src`, |
| 291 | + `${workspaceRoot}/libs/test-lib-c/nested-1/src`, |
| 292 | + `${workspaceRoot}/libs/test-lib-c/nested-2/src`, |
| 293 | + `${workspaceRoot}/libs/other/test-lib-a/src`, |
| 294 | + ]); |
| 295 | + }); |
| 296 | + it('Exclude', async () => { |
| 297 | + const paths = await getDecoratedPaths({ |
| 298 | + includeProjects: { customFilter: (p) => p.name === 'tmp-test-lib-a' }, |
| 299 | + }); |
| 300 | + expect(paths).toEqual([`${workspaceRoot}/libs/test-lib-a/src`]); |
| 301 | + }); |
| 302 | + }); |
| 303 | +}); |
0 commit comments