|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import fs from 'node:fs' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { Reflection } from '../../src/reflection.js' |
| 7 | +import { SuiteReflection } from '../../src/suite.js' |
| 8 | +import { clearCache } from '../../src/parser.js' |
| 9 | +import { NotFoundError, AmbiguousLocateError, UnsupportedSourceError } from '../../src/errors.js' |
| 10 | + |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 12 | +const fix = p => path.resolve(__dirname, '../fixtures', p) |
| 13 | + |
| 14 | +function tmp(contents, ext = '.js') { |
| 15 | + const p = path.join(os.tmpdir(), `reflection-scan-${Date.now()}-${Math.random().toString(36).slice(2)}${ext}`) |
| 16 | + fs.writeFileSync(p, contents) |
| 17 | + return p |
| 18 | +} |
| 19 | + |
| 20 | +describe('Reflection.scanFile', () => { |
| 21 | + beforeEach(() => clearCache()) |
| 22 | + |
| 23 | + it('returns suites and tests for a single-Feature file', () => { |
| 24 | + const { suites, tests } = Reflection.scanFile(fix('js/simple.scenario.js')) |
| 25 | + expect(suites).toHaveLength(1) |
| 26 | + expect(suites[0].title).toBe('Auth') |
| 27 | + expect(suites[0].line).toBeGreaterThan(0) |
| 28 | + expect(suites[0].range.start).toBeLessThan(suites[0].range.end) |
| 29 | + |
| 30 | + expect(tests.map(t => t.title)).toEqual(['login works', 'logout works']) |
| 31 | + for (const t of tests) { |
| 32 | + expect(t.suite).toBe('Auth') |
| 33 | + expect(t.file).toBe(suites[0].file) |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + it('assigns each test to its parent suite in multi-Feature files', () => { |
| 38 | + const { suites, tests } = Reflection.scanFile(fix('js/multi-suite.scenario.js')) |
| 39 | + expect(suites.map(s => s.title)).toEqual(['First', 'Second']) |
| 40 | + const bySuite = new Map() |
| 41 | + for (const t of tests) { |
| 42 | + if (!bySuite.has(t.suite)) bySuite.set(t.suite, []) |
| 43 | + bySuite.get(t.suite).push(t.title) |
| 44 | + } |
| 45 | + expect(bySuite.get('First')).toEqual(['a1', 'a2']) |
| 46 | + expect(bySuite.get('Second')).toEqual(['b1']) |
| 47 | + }) |
| 48 | + |
| 49 | + it('returns empty suites and tests for a file with neither', () => { |
| 50 | + const file = tmp('const x = 1\n') |
| 51 | + try { |
| 52 | + const { suites, tests } = Reflection.scanFile(file) |
| 53 | + expect(suites).toEqual([]) |
| 54 | + expect(tests).toEqual([]) |
| 55 | + } finally { |
| 56 | + fs.unlinkSync(file) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + it('works on .ts scenario files', () => { |
| 61 | + const { suites, tests } = Reflection.scanFile(fix('ts/simple.scenario.ts')) |
| 62 | + expect(suites.map(s => s.title)).toEqual(['Auth']) |
| 63 | + expect(tests.map(t => t.title)).toEqual(['login works', 'typed logout']) |
| 64 | + }) |
| 65 | + |
| 66 | + it('propagates UnsupportedSourceError for Gherkin .feature files', () => { |
| 67 | + expect(() => Reflection.scanFile(fix('gherkin/login.feature'))).toThrow(UnsupportedSourceError) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +describe('Reflection.forSuite auto-title', () => { |
| 72 | + beforeEach(() => clearCache()) |
| 73 | + |
| 74 | + it('auto-detects the single Feature when only one exists in the file', () => { |
| 75 | + const sur = Reflection.forSuite({ file: fix('js/simple.scenario.js') }) |
| 76 | + expect(sur).toBeInstanceOf(SuiteReflection) |
| 77 | + expect(sur.title).toBe('Auth') |
| 78 | + const block = sur.read() |
| 79 | + expect(block).toBe("Feature('Auth')") |
| 80 | + }) |
| 81 | + |
| 82 | + it('accepts a bare file path string as shorthand', () => { |
| 83 | + const sur = Reflection.forSuite(fix('js/simple.scenario.js')) |
| 84 | + expect(sur.title).toBe('Auth') |
| 85 | + }) |
| 86 | + |
| 87 | + it('lists hooks and tests after auto-detection', () => { |
| 88 | + const file = tmp( |
| 89 | + `Feature('Auto') |
| 90 | +
|
| 91 | +BeforeSuite(async () => {}) |
| 92 | +
|
| 93 | +Scenario('first', async ({ I }) => { I.amOnPage('/') }) |
| 94 | +Scenario('second', async ({ I, loginPage }) => { loginPage.open() }) |
| 95 | +`, |
| 96 | + ) |
| 97 | + try { |
| 98 | + const sur = Reflection.forSuite({ file }) |
| 99 | + expect(sur.title).toBe('Auto') |
| 100 | + expect(sur.tests.map(t => t.title)).toEqual(['first', 'second']) |
| 101 | + expect(sur.hooks.map(h => h.kind)).toEqual(['BeforeSuite']) |
| 102 | + expect(sur.dependencies.sort()).toEqual(['I', 'loginPage']) |
| 103 | + } finally { |
| 104 | + fs.unlinkSync(file) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + it('throws AmbiguousLocateError when the file has multiple Features', () => { |
| 109 | + const file = fix('js/multi-suite.scenario.js') |
| 110 | + const sur = Reflection.forSuite({ file }) |
| 111 | + let thrown |
| 112 | + try { sur.title } catch (e) { thrown = e } |
| 113 | + expect(thrown).toBeInstanceOf(AmbiguousLocateError) |
| 114 | + expect(thrown.candidates).toHaveLength(2) |
| 115 | + expect(thrown.candidates.map(c => c.title).sort()).toEqual(['First', 'Second']) |
| 116 | + }) |
| 117 | + |
| 118 | + it('disambiguates with an explicit title when multiple Features exist', () => { |
| 119 | + const sur = Reflection.forSuite({ title: 'Second', file: fix('js/multi-suite.scenario.js') }) |
| 120 | + expect(sur.title).toBe('Second') |
| 121 | + expect(sur.tests.map(t => t.title)).toEqual(['b1']) |
| 122 | + }) |
| 123 | + |
| 124 | + it('throws NotFoundError for a file with no Feature', () => { |
| 125 | + const file = tmp('const x = 1\n') |
| 126 | + try { |
| 127 | + const sur = Reflection.forSuite({ file }) |
| 128 | + expect(() => sur.title).toThrow(NotFoundError) |
| 129 | + } finally { |
| 130 | + fs.unlinkSync(file) |
| 131 | + } |
| 132 | + }) |
| 133 | +}) |
0 commit comments