Skip to content

Commit bc8211c

Browse files
authored
fix(init): missing file ext of step file when using typescript (#5615)
* fix(init): missing file ext of step file when using typescript * add more tests * add proper verification
1 parent a70c814 commit bc8211c

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

lib/command/init.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default async function (initPath, options = {}) {
206206
const stepFile = `./steps_file.${extension}`
207207
fs.writeFileSync(path.join(testsPath, stepFile), extension === 'ts' ? defaultActorTs : defaultActor)
208208

209-
config.include = { I: isTypeScript ? './steps_file' : stepFile }
209+
config.include = { I: isTypeScript ? './steps_file.ts' : stepFile }
210210

211211
print(`Steps file created at ${stepFile}`)
212212

@@ -317,6 +317,8 @@ export default async function (initPath, options = {}) {
317317
return
318318
}
319319

320+
if (process.env.CODECEPT_TEST) return
321+
320322
const generatedTest = generateTest(testsPath)
321323
if (!generatedTest) return
322324
generatedTest.then(() => {

test/runner/init_test.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import path from 'path'
44
import fs from 'fs'
55
import { mkdirp } from 'mkdirp'
66
import { fileURLToPath } from 'url'
7+
import sinon from 'sinon'
8+
import inquirer from 'inquirer'
9+
import * as initModule from '../../lib/command/init.js'
710

811
const __filename = fileURLToPath(import.meta.url)
912
const __dirname = path.dirname(__filename)
@@ -13,29 +16,24 @@ const codecept_dir = path.join(__dirname, '/../data/sandbox/configs/init')
1316
describe('Init Command', function () {
1417
this.timeout(20000)
1518

19+
let promptStub;
20+
1621
beforeEach(() => {
1722
mkdirp.sync(codecept_dir)
1823
process.env._INIT_DRY_RUN_INSTALL = true
24+
process.env.CODECEPT_TEST = 'true'
25+
promptStub = sinon.stub(inquirer, 'prompt')
1926
})
2027

2128
afterEach(() => {
2229
try {
23-
fs.unlinkSync(`${codecept_dir}/codecept.conf.ts`)
24-
fs.unlinkSync(`${codecept_dir}/steps_file.ts`)
25-
fs.unlinkSync(`${codecept_dir}/tsconfig.json`)
30+
fs.rmSync(codecept_dir, { recursive: true, force: true })
2631
} catch (e) {
2732
// continue regardless of error
2833
}
29-
30-
try {
31-
fs.unlinkSync(`${codecept_dir}/codecept.conf.js`)
32-
fs.unlinkSync(`${codecept_dir}/steps_file.js`)
33-
fs.unlinkSync(`${codecept_dir}/jsconfig.json`)
34-
} catch (e) {
35-
// continue regardless of error
36-
}
37-
34+
sinon.restore()
3835
delete process.env._INIT_DRY_RUN_INSTALL
36+
delete process.env.CODECEPT_TEST
3937
})
4038

4139
it('should have init command available and noTranslation defined', async () => {
@@ -63,4 +61,47 @@ describe('Init Command', function () {
6361
throw error
6462
}
6563
})
64+
65+
it('should initialize a JS project', async () => {
66+
// When yes: true, it skips prompts
67+
await initModule.default(codecept_dir, { yes: true })
68+
69+
fs.existsSync(path.join(codecept_dir, 'codecept.conf.js')).should.be.true
70+
fs.existsSync(path.join(codecept_dir, 'steps_file.js')).should.be.true
71+
72+
fs.readFile(path.join(codecept_dir, 'codecept.conf.js'), 'utf8', (err, data) => {
73+
if (err) {
74+
throw Error(err);
75+
return;
76+
}
77+
data.should.contain('./steps_file.js');
78+
});
79+
})
80+
81+
it('should initialize a TS project', async () => {
82+
promptStub.onCall(0).resolves({
83+
typescript: true,
84+
tests: './tests/*_test.ts',
85+
helper: 'Playwright',
86+
output: './output',
87+
})
88+
promptStub.onCall(1).resolves({
89+
Playwright_browser: 'chromium',
90+
Playwright_url: 'http://localhost',
91+
Playwright_show: false,
92+
})
93+
94+
await initModule.default(codecept_dir, { yes: false })
95+
96+
fs.existsSync(path.join(codecept_dir, 'codecept.conf.ts')).should.be.true
97+
fs.existsSync(path.join(codecept_dir, 'steps_file.ts')).should.be.true
98+
99+
fs.readFile(path.join(codecept_dir, 'codecept.conf.ts'), 'utf8', (err, data) => {
100+
if (err) {
101+
throw Error(err);
102+
return;
103+
}
104+
data.should.contain('./steps_file.ts');
105+
});
106+
})
66107
})

0 commit comments

Comments
 (0)