Skip to content

Commit

Permalink
Fix TS test running when project is first initialized with japa (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabasbalazs authored Jan 14, 2025
1 parent f4da766 commit 2702d09
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.16.0, 20.x]
node-version: ["lts/iron", "lts/jod", "latest"]

steps:
- name: Checkout code
Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
node-version: [18.16.0, 20.x]
node-version: ["lts/iron", "lts/jod", "latest"]

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Setup japa inside an existing Node.js project",
"version": "2.1.3",
"engines": {
"node": ">=18.16.0"
"node": ">=20.9.0"
},
"main": "build/index.js",
"type": "module",
Expand Down
52 changes: 50 additions & 2 deletions src/install_japa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class InstallJapa extends BaseCommand {

const testScript =
this.projectType === 'typescript'
? 'node --loader ts-node/esm --enable-source-maps bin/test.ts'
? 'node --import ts-node-maintained/register/esm --enable-source-maps bin/test.ts'
: 'node bin/test.js'

/**
Expand All @@ -289,7 +289,8 @@ export class InstallJapa extends BaseCommand {
await this.#createNewPkgJson(basename(this.destination), testScript)
await this.#installPackages([
...this.#packageToInstall.map((pkg) => `${pkg}@latest`),
'ts-node',
'ts-node-maintained',
'@swc/core',
'typescript',
])

Expand All @@ -311,6 +312,45 @@ export class InstallJapa extends BaseCommand {
await this.#installPackages(this.#packageToInstall.map((pkg) => pkg + '@latest'))
}

/**
* Create or update the package.json file based upon the user selections
*/

async #createOrUpdateTSconfig() {
const tsConfigPath = join(this.destination, 'tsconfig.json')

/**
* Create a new tsconfig.json file when missing
*/
if (!existsSync(tsConfigPath)) {
await this.#writeFile(
'tsconfig.json',
JSON.stringify(
{
compilerOptions: {
module: 'NodeNext',
},
},
null,
2
)
)
return
}

/**
* Update existing tsconfig.json file
*/
const tsConfigJson = JSON.parse(await readFile(tsConfigPath, 'utf-8'))
tsConfigJson.compilerOptions = {
...tsConfigJson.compilerOptions,
module: 'NodeNext',
}

await writeFile(tsConfigPath, JSON.stringify(tsConfigJson, null, 2))
this.logger.action('update tsconfig.json').succeeded()
}

/**
* Print final instructions to the user
*/
Expand Down Expand Up @@ -420,6 +460,14 @@ export class InstallJapa extends BaseCommand {
*/
await this.#createOrUpdatePkgJson()

/**
* Create or update the tsconfig.json file
* if ts preset is selected
*/
if (this.projectType === 'typescript') {
await this.#createOrUpdateTSconfig()
}

this.#printSuccessSticker()
}
}
45 changes: 43 additions & 2 deletions tests/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ test.group('install', (group) => {

assert.deepEqual(pkg.type, 'module')
assert.deepEqual(pkg.scripts, {
test: 'node --loader ts-node/esm --enable-source-maps bin/test.ts',
test: 'node --import ts-node-maintained/register/esm --enable-source-maps bin/test.ts',
})
})

Expand All @@ -285,7 +285,7 @@ test.group('install', (group) => {

assert.deepEqual(pkg.type, 'module')
assert.deepEqual(pkg.scripts, {
test: 'node --loader ts-node/esm --enable-source-maps bin/test.ts',
test: 'node --import ts-node-maintained/register/esm --enable-source-maps bin/test.ts',
})
assert.deepEqual(pkg.name, 'foo')
assert.deepEqual(pkg.description, 'blabla')
Expand All @@ -312,4 +312,45 @@ test.group('install', (group) => {
process.env.npm_config_user_agent = undefined
})
.disableTimeout()

test('should create tsconfig.json if ts is selected and no tsconfig exists', async ({
assert,
fs,
}) => {
const command = await kernel.create(InstallJapa, [fs.basePath])

trapPrompts(command)

await command.exec()

await assert.fileExists('tsconfig.json')
const config = await fs.contentsJson('tsconfig.json')

assert.deepEqual(config.compilerOptions.module, 'NodeNext')
})

test('should update existing tsconfig.json if exists with correct module setting', async ({
assert,
fs,
}) => {
await fs.create(
'tsconfig.json',
JSON.stringify({ compilerOptions: { target: 'ESNext', lib: ['ESNext'] } })
)

kernel.ui.switchMode('raw')

const command = await kernel.create(InstallJapa, [fs.basePath])

trapPrompts(command)

await command.exec()

await assert.fileExists('tsconfig.json')
const config = await fs.contentsJson('tsconfig.json')

assert.deepEqual(config.compilerOptions.target, 'ESNext')
assert.deepEqual(config.compilerOptions.lib[0], 'ESNext')
assert.deepEqual(config.compilerOptions.module, 'NodeNext')
})
})

0 comments on commit 2702d09

Please sign in to comment.