Skip to content

Commit

Permalink
chore: add an external package
Browse files Browse the repository at this point in the history
  • Loading branch information
fooddilsn committed Apr 27, 2023
1 parent 9406413 commit b6ebedd
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/foo/__mocks__/sample.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Mock, vi } from 'vitest'

export const main: Mock = vi.fn().mockImplementation(() => {
console.log('Fake implementation')
console.log('sample: fake implementation')
return false
})
14 changes: 14 additions & 0 deletions apps/foo/__mocks__/undici.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Mock, vi } from 'vitest'

export const request: Mock = vi.fn().mockImplementation(() => ({
body: {
json: vi.fn().mockImplementation(() => {
console.log('undici: mocked implementation')
return {
id: 12,
quote: 'Life is what happens to you while you\'re busy making other plans.',
author: 'John Lennon',
}
})
}
}))
3 changes: 2 additions & 1 deletion apps/foo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"dependencies": {
"sample": "workspace:*",
"tslib": "^2.5.0"
"tslib": "^2.5.0",
"undici": "^5.22.0"
},
"devDependencies": {
"@ducktors/tsconfig": "^0.1.1",
Expand Down
14 changes: 11 additions & 3 deletions apps/foo/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { expect, test, vi } from 'vitest'

import { app } from './index'
import { quote, boolean } from './index'

vi.mock('sample')
vi.mock('undici')

test('should use a mocked dependency', () => {
expect(app()).toBe(false)
test('should use a mocked implementation of undici', async () => {
expect(await quote()).toEqual({
quote: 'Life is what happens to you while you\'re busy making other plans.',
author: 'John Lennon',
})
})

test('should use a mocked implementation of sample', () => {
expect(boolean()).toBe(false)
})
10 changes: 9 additions & 1 deletion apps/foo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { main } from 'sample'
import { request } from 'undici'

export function app() {
export async function quote() {
const { body } = await request('https://dummyjson.com/quotes/7')
const { quote, author } = await body.json()

return { quote, author }
}

export function boolean() {
return main()
}
2 changes: 1 addition & 1 deletion packages/sample/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function main() {
console.log('Real implementation')
console.log('sample: real implementation')
return true
}
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6ebedd

Please sign in to comment.