|
| 1 | +import { Parser } from '../parser' |
| 2 | +import * as path from 'path' |
| 3 | +import { pathToPosix } from '../../util' |
| 4 | + |
| 5 | +describe('project parser - getFilesAndDependencies()', () => { |
| 6 | + it('should handle JS file with no dependencies', async () => { |
| 7 | + const parser = new Parser({}) |
| 8 | + const res = await parser.getFilesAndDependencies([path.join(__dirname, 'check-parser-fixtures', 'no-dependencies.js')]) |
| 9 | + expect(res.files).toHaveLength(1) |
| 10 | + expect(res.errors).toHaveLength(0) |
| 11 | + }) |
| 12 | + |
| 13 | + it('should handle JS file with dependencies', async () => { |
| 14 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'simple-example', ...filepath) |
| 15 | + const parser = new Parser({}) |
| 16 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.js')]) |
| 17 | + const expectedFiles = [ |
| 18 | + 'dep1.js', |
| 19 | + 'dep2.js', |
| 20 | + 'dep3.js', |
| 21 | + 'entrypoint.js', |
| 22 | + 'module-package/main.js', |
| 23 | + 'module-package/package.json', |
| 24 | + 'module/index.js', |
| 25 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 26 | + |
| 27 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 28 | + expect(res.errors).toHaveLength(0) |
| 29 | + }) |
| 30 | + |
| 31 | + it('Should not repeat files if duplicated', async () => { |
| 32 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'simple-example', ...filepath) |
| 33 | + const parser = new Parser({}) |
| 34 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.js'), toAbsolutePath('*.js')]) |
| 35 | + const expectedFiles = [ |
| 36 | + 'dep1.js', |
| 37 | + 'dep2.js', |
| 38 | + 'dep3.js', |
| 39 | + 'entrypoint.js', |
| 40 | + 'module-package/main.js', |
| 41 | + 'module-package/package.json', |
| 42 | + 'module/index.js', |
| 43 | + 'unreachable.js', |
| 44 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 45 | + |
| 46 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 47 | + expect(res.errors).toHaveLength(0) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should not fail on a non-existing directory', async () => { |
| 51 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'simple-example-that-does-not-exist', ...filepath) |
| 52 | + const parser = new Parser({}) |
| 53 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('/')]) |
| 54 | + expect(res.files).toHaveLength(0) |
| 55 | + expect(res.errors).toHaveLength(0) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should parse the cli in less than 400ms', async () => { |
| 59 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, '../../../', ...filepath) |
| 60 | + const startTimestamp = Date.now().valueOf() |
| 61 | + const res = await new Parser({}).getFilesAndDependencies([toAbsolutePath('/index.ts')]) |
| 62 | + const endTimestamp = Date.now().valueOf() |
| 63 | + expect(res.files).not.toHaveLength(0) |
| 64 | + expect(res.errors).toHaveLength(0) |
| 65 | + const isCI = process.env.CI === 'true' |
| 66 | + expect(endTimestamp - startTimestamp).toBeLessThan(isCI ? 2000 : 400) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should handle JS file with dependencies glob patterns', async () => { |
| 70 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'simple-example', ...filepath) |
| 71 | + const parser = new Parser({}) |
| 72 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('*.js'), toAbsolutePath('*.json')]) |
| 73 | + const expectedFiles = [ |
| 74 | + 'dep1.js', |
| 75 | + 'dep2.js', |
| 76 | + 'dep3.js', |
| 77 | + 'entrypoint.js', |
| 78 | + 'module-package/main.js', |
| 79 | + 'module-package/package.json', |
| 80 | + 'module/index.js', |
| 81 | + 'unreachable.js', |
| 82 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 83 | + |
| 84 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 85 | + expect(res.errors).toHaveLength(0) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should parse typescript dependencies', async () => { |
| 89 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'typescript-example', ...filepath) |
| 90 | + const parser = new Parser({}) |
| 91 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.ts')]) |
| 92 | + const expectedFiles = [ |
| 93 | + 'dep1.ts', |
| 94 | + 'dep2.ts', |
| 95 | + 'dep3.ts', |
| 96 | + 'dep4.js', |
| 97 | + 'dep5.ts', |
| 98 | + 'dep6.ts', |
| 99 | + 'entrypoint.ts', |
| 100 | + 'module-package/main.js', |
| 101 | + 'module-package/package.json', |
| 102 | + 'module/index.ts', |
| 103 | + 'pages/external.first.page.js', |
| 104 | + 'pages/external.second.page.ts', |
| 105 | + 'type.ts', |
| 106 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 107 | + |
| 108 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 109 | + expect(res.errors).toHaveLength(0) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should parse typescript dependencies using tsconfig', async () => { |
| 113 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'tsconfig-paths-sample-project', ...filepath) |
| 114 | + const parser = new Parser({}) |
| 115 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('src', 'entrypoint.ts')]) |
| 116 | + const expectedFiles = [ |
| 117 | + 'lib1/file1.ts', |
| 118 | + 'lib1/file2.ts', |
| 119 | + 'lib1/folder/file1.ts', |
| 120 | + 'lib1/folder/file2.ts', |
| 121 | + 'lib1/index.ts', |
| 122 | + 'lib1/package.json', |
| 123 | + 'lib1/tsconfig.json', |
| 124 | + 'lib2/index.ts', |
| 125 | + 'lib3/foo/bar.ts', |
| 126 | + 'src/entrypoint.ts', |
| 127 | + 'tsconfig.json', |
| 128 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 129 | + |
| 130 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 131 | + expect(res.errors).toHaveLength(0) |
| 132 | + }) |
| 133 | + |
| 134 | + it('should not include tsconfig if not needed', async () => { |
| 135 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'tsconfig-paths-unused', ...filepath) |
| 136 | + const parser = new Parser({}) |
| 137 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('src', 'entrypoint.ts')]) |
| 138 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual([pathToPosix(toAbsolutePath('src', 'entrypoint.ts'))]) |
| 139 | + expect(res.errors).toHaveLength(0) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should support importing ts extensions if allowed', async () => { |
| 143 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'tsconfig-allow-importing-ts-extensions', ...filepath) |
| 144 | + const parser = new Parser({}) |
| 145 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('src', 'entrypoint.ts')]) |
| 146 | + const expectedFiles = [ |
| 147 | + 'src/dep1.ts', |
| 148 | + 'src/dep2.ts', |
| 149 | + 'src/dep3.ts', |
| 150 | + 'src/entrypoint.ts', |
| 151 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 152 | + |
| 153 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 154 | + expect(res.errors).toHaveLength(0) |
| 155 | + }) |
| 156 | + |
| 157 | + it('should not import TS files from a JS file', async () => { |
| 158 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'no-import-ts-from-js', ...filepath) |
| 159 | + const parser = new Parser({}) |
| 160 | + expect.assertions(1) |
| 161 | + try { |
| 162 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 163 | + await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.js')]) |
| 164 | + } catch (err) { |
| 165 | + expect(err).toMatchObject({ |
| 166 | + missingFiles: [ |
| 167 | + pathToPosix(toAbsolutePath('dep1')), |
| 168 | + pathToPosix(toAbsolutePath('dep1.ts')), |
| 169 | + pathToPosix(toAbsolutePath('dep1.js')), |
| 170 | + ], |
| 171 | + }) |
| 172 | + } |
| 173 | + }) |
| 174 | + |
| 175 | + it('should import JS files from a TS file', async () => { |
| 176 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'import-js-from-ts', ...filepath) |
| 177 | + const parser = new Parser({}) |
| 178 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.ts')]) |
| 179 | + const expectedFiles = [ |
| 180 | + 'dep1.js', |
| 181 | + 'dep2.js', |
| 182 | + 'dep3.ts', |
| 183 | + 'entrypoint.ts', |
| 184 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 185 | + |
| 186 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 187 | + }) |
| 188 | + |
| 189 | + it('should handle ES Modules', async () => { |
| 190 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'esmodules-example', ...filepath) |
| 191 | + const parser = new Parser({}) |
| 192 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.js')]) |
| 193 | + const expectedFiles = [ |
| 194 | + 'dep1.js', |
| 195 | + 'dep2.js', |
| 196 | + 'dep3.js', |
| 197 | + 'dep5.js', |
| 198 | + 'dep6.js', |
| 199 | + 'entrypoint.js', |
| 200 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 201 | + |
| 202 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 203 | + }) |
| 204 | + |
| 205 | + it('should handle Common JS and ES Modules', async () => { |
| 206 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'common-esm-example', ...filepath) |
| 207 | + const parser = new Parser({}) |
| 208 | + const res = await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.mjs')]) |
| 209 | + const expectedFiles = [ |
| 210 | + 'dep1.js', |
| 211 | + 'dep2.mjs', |
| 212 | + 'dep3.mjs', |
| 213 | + 'dep4.mjs', |
| 214 | + 'dep5.mjs', |
| 215 | + 'dep6.mjs', |
| 216 | + 'entrypoint.mjs', |
| 217 | + ].map(file => pathToPosix(toAbsolutePath(file))) |
| 218 | + |
| 219 | + expect(res.files.map(file => pathToPosix(file)).sort()).toEqual(expectedFiles) |
| 220 | + }) |
| 221 | + |
| 222 | + it('should handle node: prefix for built-ins', async () => { |
| 223 | + const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'builtin-with-node-prefix', ...filepath) |
| 224 | + const parser = new Parser({}) |
| 225 | + await parser.getFilesAndDependencies([toAbsolutePath('entrypoint.ts')]) |
| 226 | + }) |
| 227 | + |
| 228 | + /* |
| 229 | + * There is an unhandled edge-case when require() is reassigned. |
| 230 | + * Even though the check might execute fine, we throw an error for a missing dependency. |
| 231 | + * We could address this by keeping track of assignments as we walk the AST. |
| 232 | + */ |
| 233 | + it.skip('should ignore cases where require is reassigned', async () => { |
| 234 | + const entrypoint = path.join(__dirname, 'check-parser-fixtures', 'reassign-require.js') |
| 235 | + const parser = new Parser({}) |
| 236 | + await parser.getFilesAndDependencies([entrypoint]) |
| 237 | + }) |
| 238 | + |
| 239 | + // Checks run on Checkly are wrapped to support top level await. |
| 240 | + // For consistency with checks created via the UI, the CLI should support this as well. |
| 241 | + it('should allow top-level await', async () => { |
| 242 | + const entrypoint = path.join(__dirname, 'check-parser-fixtures', 'top-level-await.js') |
| 243 | + const parser = new Parser({}) |
| 244 | + await parser.getFilesAndDependencies([entrypoint]) |
| 245 | + }) |
| 246 | + |
| 247 | + it('should allow top-level await in TypeScript', async () => { |
| 248 | + const entrypoint = path.join(__dirname, 'check-parser-fixtures', 'top-level-await.ts') |
| 249 | + const parser = new Parser({}) |
| 250 | + await parser.getFilesAndDependencies([entrypoint]) |
| 251 | + }) |
| 252 | +}) |
0 commit comments