Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default async function (initPath, options = {}) {
const stepFile = `./steps_file.${extension}`
fs.writeFileSync(path.join(testsPath, stepFile), extension === 'ts' ? defaultActorTs : defaultActor)

config.include = { I: isTypeScript ? './steps_file' : stepFile }
config.include = { I: isTypeScript ? './steps_file.ts' : stepFile }

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

Expand Down Expand Up @@ -317,6 +317,8 @@ export default async function (initPath, options = {}) {
return
}

if (process.env.CODECEPT_TEST) return

const generatedTest = generateTest(testsPath)
if (!generatedTest) return
generatedTest.then(() => {
Expand Down
65 changes: 53 additions & 12 deletions test/runner/init_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import path from 'path'
import fs from 'fs'
import { mkdirp } from 'mkdirp'
import { fileURLToPath } from 'url'
import sinon from 'sinon'
import inquirer from 'inquirer'
import * as initModule from '../../lib/command/init.js'

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

let promptStub;

beforeEach(() => {
mkdirp.sync(codecept_dir)
process.env._INIT_DRY_RUN_INSTALL = true
process.env.CODECEPT_TEST = 'true'
promptStub = sinon.stub(inquirer, 'prompt')
})

afterEach(() => {
try {
fs.unlinkSync(`${codecept_dir}/codecept.conf.ts`)
fs.unlinkSync(`${codecept_dir}/steps_file.ts`)
fs.unlinkSync(`${codecept_dir}/tsconfig.json`)
fs.rmSync(codecept_dir, { recursive: true, force: true })
} catch (e) {
// continue regardless of error
}

try {
fs.unlinkSync(`${codecept_dir}/codecept.conf.js`)
fs.unlinkSync(`${codecept_dir}/steps_file.js`)
fs.unlinkSync(`${codecept_dir}/jsconfig.json`)
} catch (e) {
// continue regardless of error
}

sinon.restore()
delete process.env._INIT_DRY_RUN_INSTALL
delete process.env.CODECEPT_TEST
})

it('should have init command available and noTranslation defined', async () => {
Expand Down Expand Up @@ -63,4 +61,47 @@ describe('Init Command', function () {
throw error
}
})

it('should initialize a JS project', async () => {
// When yes: true, it skips prompts
await initModule.default(codecept_dir, { yes: true })

fs.existsSync(path.join(codecept_dir, 'codecept.conf.js')).should.be.true
fs.existsSync(path.join(codecept_dir, 'steps_file.js')).should.be.true

fs.readFile(path.join(codecept_dir, 'codecept.conf.js'), 'utf8', (err, data) => {
if (err) {
throw Error(err);
return;
}
data.should.contain('./steps_file.js');
});
})

it('should initialize a TS project', async () => {
promptStub.onCall(0).resolves({
typescript: true,
tests: './tests/*_test.ts',
helper: 'Playwright',
output: './output',
})
promptStub.onCall(1).resolves({
Playwright_browser: 'chromium',
Playwright_url: 'http://localhost',
Playwright_show: false,
})

await initModule.default(codecept_dir, { yes: false })

fs.existsSync(path.join(codecept_dir, 'codecept.conf.ts')).should.be.true
fs.existsSync(path.join(codecept_dir, 'steps_file.ts')).should.be.true

fs.readFile(path.join(codecept_dir, 'codecept.conf.ts'), 'utf8', (err, data) => {
if (err) {
throw Error(err);
return;
}
data.should.contain('./steps_file.ts');
});
})
})
Loading