Skip to content

Commit

Permalink
test: added tests (ipfs#1273)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed May 17, 2023
1 parent b256417 commit d178e46
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/document-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ const tasks = new Listr(
* @param {Task} task
*/
task: async (ctx, task) => {
let configPath = './tsconfig-doc-check.aegir.json'
const configPath = './tsconfig-doc-check.aegir.json'

let userTSConfig = {}
let markdownFiles = ['README.md']

if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') {
configPath = `${ctx.tsConfigPath}/tsconfig.json`
userTSConfig = readJson(`${ctx.tsConfigPath}/tsconfig.json`)
} else {
userTSConfig = readJson(fromRoot('tsconfig.json'))
}

if (ctx.inputFiles) {
markdownFiles = await globby(ctx.inputFiles)
}

const userTSConfig = readJson(fromRoot('tsconfig.json'))

try {
fs.writeJsonSync(
configPath,
Expand Down Expand Up @@ -66,8 +68,6 @@ const tasks = new Listr(
console.log(formatCode(result.snippet, result.linesWithErrors))
}
})
} catch (error) {
console.error('Error complining Typescript snippets ', error)
} finally {
fs.removeSync(configPath)
fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo'))
Expand Down
37 changes: 37 additions & 0 deletions test/document-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env mocha */

import { createRequire } from 'module'
import path from 'path'
import { fileURLToPath } from 'url'
import { execa } from 'execa'
import { expect } from '../utils/chai.js'

const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const bin = require.resolve('../src/index.js')

describe('document check', () => {
it('should fail for errant typescript in docs', async () => {
const cwd = path.join(__dirname, 'fixtures/document-check/ts-fail')

await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`]))
.to.eventually.be.rejected()
.with.property('stdout')
.that.include('Error compiling example code block 1')
})

it('should pass for correct typescript in docs', async () => {
const cwd = path.join(__dirname, 'fixtures/document-check/pass')

await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`])).to.eventually.be.fulfilled()
})

it('should fail for missing tsconfig.json', async () => {
const cwd = path.join(__dirname, 'fixtures/document-check/tsconfig-fail')

await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`]))
.to.eventually.be.rejected()
.with.property('stderr')
.that.include('no such file or directory')
})
})
3 changes: 3 additions & 0 deletions test/fixtures/document-check/pass/GOODREADME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ts
export const a = 1;
```
14 changes: 14 additions & 0 deletions test/fixtures/document-check/pass/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./../../../../src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": true,
"isolatedModules": true
},
"include": [
"package.json",
"src",
"test",
"utils"
]
}
3 changes: 3 additions & 0 deletions test/fixtures/document-check/ts-fail/ANOTHERBADREADME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```typescript
still.bad.code
```
3 changes: 3 additions & 0 deletions test/fixtures/document-check/ts-fail/BADREADME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```typescript
wrong.code()
```
13 changes: 13 additions & 0 deletions test/fixtures/document-check/ts-fail/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./../../../../src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": true
},
"include": [
"package.json",
"src",
"test",
"utils"
]
}
3 changes: 3 additions & 0 deletions test/fixtures/document-check/tsconfig-fail/GOODREADME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ts
export const a = 1;
```
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './docs.js'
import './lint.js'
import './fixtures.js'
import './dependants.js'
import './document-check.js'
import './dependency-check.js'
import './utils/echo-server.js'
import './utils/get-port.js'
Expand Down

0 comments on commit d178e46

Please sign in to comment.