Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
test: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 8, 2024
1 parent 27b7619 commit 312a9bb
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
- name: Check types
run: pnpm turbo types:check
- name: Run tests
run: pnpm turbo test
run: pnpm test
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
"@scalar/cli": "pnpm --filter @scalar/cli run @scalar/cli",
"test": "pnpm -r test",
"test": "vitest",
"lint": "pnpm -r lint",
"format": "pnpm -r format",
"format:check": "pnpm -r format:check",
Expand All @@ -15,6 +15,7 @@
"@changesets/cli": "^2.27.1"
},
"devDependencies": {
"turbo": "^1.12.3"
"turbo": "^1.12.3",
"vitest": "^1.2.2"
}
}
4 changes: 1 addition & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"scripts": {
"@scalar/cli": "pnpm vite-node src/index.ts",
"test": "vitest",
"lint": "pnpm dlx @biomejs/biome lint .",
"format": "pnpm dlx @biomejs/biome check . --apply",
"format:check": "pnpm dlx @biomejs/biome check .",
Expand Down Expand Up @@ -44,8 +43,7 @@
"prettyjson": "^1.2.5",
"prompts": "^2.4.2",
"toml-js": "^0.0.8",
"vite-node": "^1.2.2",
"vitest": "^1.2.2"
"vite-node": "^1.2.2"
},
"devDependencies": {
"@biomejs/biome": "1.5.3",
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/scalar.toml

This file was deleted.

4 changes: 2 additions & 2 deletions packages/cli/src/commands/format/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe('format', () => {
const notWellFormattedJson = '{"foo": "bar"}'

// Create JSON file
const jsonFile = './src/commands/format/temp.json'
const jsonFile = './packages/cli/src/commands/format/temp.json'
fs.writeFileSync(jsonFile, notWellFormattedJson)

// Format
const [exitCode, logs] = ScalarCli()
.setCwd(path.resolve('./'))
.invoke(['format', './src/commands/format/temp.json'])
.invoke(['format', './packages/cli/src/commands/format/temp.json'])

// Output
logs.should.contain('File formatted')
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/commands/init/InitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import { Command } from 'commander'
import kleur from 'kleur'
import prompts from 'prompts'
import toml from 'toml-js'
import path from 'node:path'

export function InitCommand() {
const cmd = new Command('init')


cmd.description('Create a new `scalar.toml` file')
cmd.option('-f, --file [file]', 'your OpenAPI file')
cmd.action(async ({ file }) => {
// Path to `scalar.toml` file
const configFile = path.resolve('scalar.toml')

// Check if `scalar.toml` already exists
if (fs.existsSync('scalar.toml')) {
if (fs.existsSync(configFile)) {
console.warn(kleur.yellow('A `scalar.toml` file already exists.'))
console.log()

Expand Down Expand Up @@ -66,7 +71,7 @@ export function InitCommand() {
console.log()

// Create `scalar.toml` file
fs.writeFileSync('scalar.toml', content)
fs.writeFileSync(configFile, content)

console.log(kleur.green('Created a new project configuration.'))
console.log(
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/commands/init/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'
import { ScalarCli } from '../../../tests/invoke-cli'
import path from 'node:path'
import fs from 'node:fs'

describe('init', () => {
it('creates a config file', () => {
// Delete config file if it exists
const configFile = './scalar.toml'

if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile)
}

// Doesn’t exist
expect(fs.existsSync(configFile)).toBe(false)

// Create config file
const [exitCode, logs] = ScalarCli()
.setCwd(path.resolve('./'))
.invoke(['init', '--file', './packages/cli/src/commands/validate/valid.json'])

// Output
logs.should.contain('./packages/cli/src/commands/validate/valid.json')

// File exists
expect(fs.existsSync(configFile)).toBe(true)
expect(exitCode).toBe(0)

fs.unlinkSync(configFile)
})
})
4 changes: 2 additions & 2 deletions packages/cli/src/commands/validate/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('validate', () => {
it('validates the given file', () => {
const [exitCode, logs] = ScalarCli()
.setCwd(path.resolve('./'))
.invoke(['validate', './src/commands/validate/valid.json'])
.invoke(['validate', './packages/cli/src/commands/validate/valid.json'])

logs.should.contain('OpenAPI 3.1')
expect(exitCode).toBe(0)
Expand All @@ -15,7 +15,7 @@ describe('validate', () => {
it('shows errors for invalid file', () => {
const [exitCode, logs] = ScalarCli()
.setCwd(path.resolve('./'))
.invoke(['validate', './src/commands/validate/invalid.json'])
.invoke(['validate', './packages/cli/src/commands/validate/invalid.json'])

logs.should.contain('Cannot find supported swagger/openapi version')
logs.should.not.contain('OpenAPI 3.1')
Expand Down
Loading

0 comments on commit 312a9bb

Please sign in to comment.