|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { runVitestCli } from '../../test-utils' |
| 3 | + |
| 4 | +const fixturePath = './fixtures/location-filters' |
| 5 | + |
| 6 | +describe('location filter with list command', () => { |
| 7 | + test('finds test at correct line number', async () => { |
| 8 | + const { stdout, stderr } = await runVitestCli( |
| 9 | + 'list', |
| 10 | + `-r=${fixturePath}`, |
| 11 | + `${fixturePath}/basic.test.ts:5`, |
| 12 | + ) |
| 13 | + |
| 14 | + expect(stdout).toMatchInlineSnapshot(` |
| 15 | + "basic.test.ts > basic suite > inner suite > some test |
| 16 | + " |
| 17 | + `) |
| 18 | + expect(stderr).toEqual('') |
| 19 | + }) |
| 20 | + |
| 21 | + test('handles file with a dash in the name', async () => { |
| 22 | + const { stdout, stderr } = await runVitestCli( |
| 23 | + 'list', |
| 24 | + `-r=${fixturePath}`, |
| 25 | + `${fixturePath}/math-with-dashes-in-name.test.ts:3`, |
| 26 | + ) |
| 27 | + |
| 28 | + expect(stdout).toMatchInlineSnapshot(` |
| 29 | + "math-with-dashes-in-name.test.ts > 1 plus 1 |
| 30 | + " |
| 31 | + `) |
| 32 | + expect(stderr).toEqual('') |
| 33 | + }) |
| 34 | + |
| 35 | + test('reports not found test', async () => { |
| 36 | + const { stdout, stderr } = await runVitestCli( |
| 37 | + 'list', |
| 38 | + `-r=${fixturePath}`, |
| 39 | + `${fixturePath}/basic.test.ts:99`, |
| 40 | + ) |
| 41 | + |
| 42 | + expect(stdout).toEqual('') |
| 43 | + expect(stderr).toMatchInlineSnapshot(` |
| 44 | + "Error: No test found in basic.test.ts in line 99 |
| 45 | + " |
| 46 | + `) |
| 47 | + }) |
| 48 | + |
| 49 | + test('reports multiple not found tests', async () => { |
| 50 | + const { stdout, stderr } = await runVitestCli( |
| 51 | + 'list', |
| 52 | + `-r=${fixturePath}`, |
| 53 | + `${fixturePath}/basic.test.ts:5`, |
| 54 | + `${fixturePath}/basic.test.ts:12`, |
| 55 | + `${fixturePath}/basic.test.ts:99`, |
| 56 | + ) |
| 57 | + |
| 58 | + expect(stdout).toEqual('') |
| 59 | + expect(stderr).toMatchInlineSnapshot(` |
| 60 | + "Error: No test found in basic.test.ts in lines 12, 99 |
| 61 | + " |
| 62 | + `) |
| 63 | + }) |
| 64 | + |
| 65 | + test('errors if range location is provided', async () => { |
| 66 | + const { stdout, stderr } = await runVitestCli( |
| 67 | + 'list', |
| 68 | + `-r=${fixturePath}`, |
| 69 | + `${fixturePath}/a/file/that/doesnt/exit:10-15`, |
| 70 | + ) |
| 71 | + |
| 72 | + expect(stdout).toEqual('') |
| 73 | + expect(stderr).toContain('Collect Error') |
| 74 | + expect(stderr).toContain('RangeLocationFilterProvidedError') |
| 75 | + }) |
| 76 | + |
| 77 | + test('parses file with a colon and dash in the name correctly', async () => { |
| 78 | + const { stdout, stderr } = await runVitestCli( |
| 79 | + 'list', |
| 80 | + `-r=${fixturePath}`, |
| 81 | + `${fixturePath}/:a/file/that/doesn-t/exit:10`, |
| 82 | + ) |
| 83 | + |
| 84 | + expect(stdout).toEqual('') |
| 85 | + // shouldn't get a range location error |
| 86 | + expect(stderr).not.toContain('Error: Found "-"') |
| 87 | + }) |
| 88 | + |
| 89 | + test('erorrs if includeTaskLocation is not enabled', async () => { |
| 90 | + const { stdout, stderr } = await runVitestCli( |
| 91 | + 'list', |
| 92 | + `-r=${fixturePath}`, |
| 93 | + '--config=no-task-location.config.ts', |
| 94 | + `${fixturePath}/a/file/that/doesnt/exist:5`, |
| 95 | + ) |
| 96 | + |
| 97 | + expect(stdout).toEqual('') |
| 98 | + expect(stderr).toContain('Collect Error') |
| 99 | + expect(stderr).toContain('IncludeTaskLocationDisabledError') |
| 100 | + }) |
| 101 | + |
| 102 | + test('fails on part of filename with location filter', async () => { |
| 103 | + const { stdout, stderr } = await runVitestCli( |
| 104 | + 'list', |
| 105 | + `-r=${fixturePath}`, |
| 106 | + `math:999`, |
| 107 | + ) |
| 108 | + |
| 109 | + expect(stdout).toEqual('') |
| 110 | + expect(stderr).toContain('Collect Error') |
| 111 | + expect(stderr).toContain('LocationFilterFileNotFoundError') |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +describe('location filter with run command', () => { |
| 116 | + test('finds test at correct line number', async () => { |
| 117 | + const { stdout, stderr } = await runVitestCli( |
| 118 | + 'run', |
| 119 | + `-r=${fixturePath}`, |
| 120 | + `${fixturePath}/math.test.ts:3`, |
| 121 | + ) |
| 122 | + |
| 123 | + // expect(`${stdout}\n--------------------\n${stderr}`).toEqual('') |
| 124 | + |
| 125 | + expect(stdout).contain('1 passed') |
| 126 | + expect(stdout).contain('1 skipped') |
| 127 | + expect(stderr).toEqual('') |
| 128 | + }) |
| 129 | + |
| 130 | + test('handles file with a dash in the name', async () => { |
| 131 | + const { stdout, stderr } = await runVitestCli( |
| 132 | + 'run', |
| 133 | + `-r=${fixturePath}`, |
| 134 | + `${fixturePath}/math-with-dashes-in-name.test.ts:3`, |
| 135 | + ) |
| 136 | + |
| 137 | + expect(stdout).contain('1 passed') |
| 138 | + expect(stdout).contain('1 skipped') |
| 139 | + expect(stderr).toEqual('') |
| 140 | + }) |
| 141 | + |
| 142 | + test('reports not found test', async () => { |
| 143 | + const { stdout, stderr } = await runVitestCli( |
| 144 | + 'run', |
| 145 | + `-r=${fixturePath}`, |
| 146 | + `${fixturePath}/basic.test.ts:99`, |
| 147 | + ) |
| 148 | + |
| 149 | + expect(stdout).toContain('4 skipped') |
| 150 | + expect(stderr).toContain('Error: No test found in basic.test.ts in line 99') |
| 151 | + }) |
| 152 | + |
| 153 | + test('reports multiple not found tests', async () => { |
| 154 | + const { stdout, stderr } = await runVitestCli( |
| 155 | + 'run', |
| 156 | + `-r=${fixturePath}`, |
| 157 | + `${fixturePath}/basic.test.ts:5`, |
| 158 | + `${fixturePath}/basic.test.ts:12`, |
| 159 | + `${fixturePath}/basic.test.ts:99`, |
| 160 | + ) |
| 161 | + |
| 162 | + expect(stdout).toContain('4 skipped') |
| 163 | + expect(stderr).toContain('Error: No test found in basic.test.ts in lines 12, 99') |
| 164 | + }) |
| 165 | + |
| 166 | + test('errors if range location is provided', async () => { |
| 167 | + const { stderr } = await runVitestCli( |
| 168 | + 'run', |
| 169 | + `-r=${fixturePath}`, |
| 170 | + `${fixturePath}/a/file/that/doesnt/exit:10-15`, |
| 171 | + ) |
| 172 | + |
| 173 | + expect(stderr).toContain('Error: Found "-"') |
| 174 | + }) |
| 175 | + |
| 176 | + test('parses file with a colon and dash in the name correctly', async () => { |
| 177 | + const { stderr } = await runVitestCli( |
| 178 | + 'run', |
| 179 | + `-r=${fixturePath}`, |
| 180 | + `${fixturePath}/:a/file/that/doesn-t/exit:10`, |
| 181 | + ) |
| 182 | + |
| 183 | + // shouldn't get a range location error |
| 184 | + expect(stderr).not.toContain('Error: Found "-"') |
| 185 | + }) |
| 186 | + |
| 187 | + test('errors if includeTaskLocation is not enabled', async () => { |
| 188 | + const { stderr } = await runVitestCli( |
| 189 | + 'run', |
| 190 | + `-r=${fixturePath}`, |
| 191 | + `--config=no-task-location.config.ts`, |
| 192 | + `${fixturePath}/a/file/that/doesnt/exist:5`, |
| 193 | + ) |
| 194 | + |
| 195 | + expect(stderr).toMatchInlineSnapshot(` |
| 196 | + "Error: Recieved line number filters while \`includeTaskLocation\` option is disabled |
| 197 | + " |
| 198 | + `) |
| 199 | + }) |
| 200 | + |
| 201 | + test('fails on part of filename with location filter', async () => { |
| 202 | + const { stdout, stderr } = await runVitestCli( |
| 203 | + 'run', |
| 204 | + `-r=${fixturePath}`, |
| 205 | + `math:999`, |
| 206 | + ) |
| 207 | + |
| 208 | + expect(stdout).not.contain('math.test.ts') |
| 209 | + expect(stdout).not.contain('math-with-dashes-in-name.test.ts') |
| 210 | + expect(stderr).toMatchInlineSnapshot(` |
| 211 | + "Error: Couldn't find file math. Note when specifying the test location you have to specify the full test filename. |
| 212 | + " |
| 213 | + `) |
| 214 | + }) |
| 215 | +}) |
0 commit comments