Skip to content

Commit

Permalink
[v0.36] Adds ability to test api functions (#3207)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
  • Loading branch information
dac09 and peterp committed Aug 12, 2021
1 parent e1e9aba commit 5c19235
Show file tree
Hide file tree
Showing 53 changed files with 627 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logger } from 'src/lib/logger'

export const handler = async (event, context) => {
logger.info('Invoked healtz function')

return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'healthz function',
}),
}
}
15 changes: 15 additions & 0 deletions __fixtures__/example-todo-main/api/src/functions/invalid/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logger } from 'src/lib/logger'

export const handler = async (event, context) => {
logger.info('Invoked x function')

return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'x function',
}),
}
}
Empty file.
15 changes: 15 additions & 0 deletions __fixtures__/example-todo-main/api/src/functions/nested/nested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logger } from 'src/lib/logger'

export const handler = async (event, context) => {
logger.info('Invoked nested function')

return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'nested function',
}),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'HELLO from X'
1 change: 1 addition & 0 deletions packages/api/src/webhooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
VerifyOptions,
WebhookVerificationError,
DEFAULT_WEBHOOK_SECRET,
SupportedVerifierTypes,
} from '../auth/verifiers'

export const DEFAULT_WEBHOOK_SIGNATURE_HEADER = 'RW-WEBHOOK-SIGNATURE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default { title: 'Cells/UserProfileCell' }
`;

exports[`creates a cell test with a camelCase word name 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './UserProfileCell'
import { standard } from './UserProfileCell.mock'
Expand All @@ -364,7 +364,7 @@ describe('UserProfileCell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
Expand All @@ -377,7 +377,7 @@ describe('UserProfileCell', () => {
`;

exports[`creates a cell test with a kebabCase word name 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './UserProfileCell'
import { standard } from './UserProfileCell.mock'
Expand All @@ -403,7 +403,7 @@ describe('UserProfileCell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
Expand All @@ -416,7 +416,7 @@ describe('UserProfileCell', () => {
`;

exports[`creates a cell test with a multi word name 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './UserProfileCell'
import { standard } from './UserProfileCell.mock'
Expand All @@ -442,7 +442,7 @@ describe('UserProfileCell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
Expand All @@ -455,7 +455,7 @@ describe('UserProfileCell', () => {
`;

exports[`creates a cell test with a single word name 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './UserCell'
import { standard } from './UserCell.mock'
Expand All @@ -481,7 +481,7 @@ describe('UserCell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
Expand All @@ -494,7 +494,7 @@ describe('UserCell', () => {
`;

exports[`creates a cell test with a snakeCase word name 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './UserProfileCell'
import { standard } from './UserProfileCell.mock'
Expand All @@ -520,7 +520,7 @@ describe('UserProfileCell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
it('renders Success successfully', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing'
import { render, screen } from '@redwoodjs/testing/web'
import { Loading, Empty, Failure, Success } from './${pascalName}Cell'
import { standard } from './${pascalName}Cell.mock'

Expand All @@ -24,7 +24,7 @@ describe('${pascalName}Cell', () => {
// When you're ready to test the actual output of your component render
// you could test that, for example, certain text is present:
//
// 1. import { screen } from '@redwoodjs/testing'
// 1. import { screen } from '@redwoodjs/testing/web'
// 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()

it('renders Success successfully', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default TypescriptUser
`;

exports[`creates a TS component and test 2`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import TypescriptUser from './TypescriptUser'
Expand Down Expand Up @@ -55,7 +55,7 @@ export default { title: 'Components/UserProfile' }
`;

exports[`creates a multi word component test 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import UserProfile from './UserProfile'
Expand Down Expand Up @@ -95,7 +95,7 @@ export default { title: 'Components/User' }
`;

exports[`creates a single word component test 1`] = `
"import { render } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing/web'
import User from './User'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@redwoodjs/testing'
import { render } from '@redwoodjs/testing/web'

import ${pascalName} from './${pascalName}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,40 @@ export const handler = async (event, context) => {
}
"
`;
exports[`creates a single word function file: Scenario snapshot 1`] = `
"export const standard = defineScenario({
// Define the \\"fixture\\" to write into your test database here
// See guide: https://redwoodjs.com/docs/testing#scenarios
})
"
`;
exports[`creates a single word function file: Test snapshot 1`] = `
"import { mockHttpEvent } from '@redwoodjs/testing/api'
import { handler } from './foo'
describe('foo function', () => {
it('Should respond with 200', async () => {
const httpEvent = mockHttpEvent({
queryStringParameters: {
id: '42', // Add parameters here
},
})
const response = await handler(httpEvent, null)
const { data } = JSON.parse(response.body)
expect(response.statusCode).toBe(200)
expect(data).toBe('foo function')
})
// You can also use scenarios to test your api functions
// See guide here: https://redwoodjs.com/docs/testing#scenarios
// scenario('Scenario test', async () => {
//
// })
})
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let typescriptFiles: WordFilesType
beforeAll(() => {
singleWordDefaultFiles = functionGenerator.files({
name: 'foo',
tests: true,
})
multiWordDefaultFiles = functionGenerator.files({
name: 'send-mail',
Expand All @@ -30,40 +31,76 @@ beforeAll(() => {
})
})

test('returns exactly 1 file', () => {
expect(Object.keys(singleWordDefaultFiles).length).toEqual(1)
test('returns tests, scenario and function file', () => {
const fileNames = Object.keys(singleWordDefaultFiles)
expect(fileNames.length).toEqual(3)

expect(fileNames).toEqual(
expect.arrayContaining([
expect.stringContaining('foo.js'),
expect.stringContaining('foo.test.js'),
expect.stringContaining('foo.scenarios.js'),
])
)
})

test('creates a single word function file', () => {
expect(
singleWordDefaultFiles[
path.normalize('/path/to/project/api/src/functions/foo.js')
path.normalize('/path/to/project/api/src/functions/foo/foo.js')
]
).toMatchSnapshot()

expect(
singleWordDefaultFiles[
path.normalize('/path/to/project/api/src/functions/foo/foo.test.js')
]
).toMatchSnapshot('Test snapshot')

expect(
singleWordDefaultFiles[
path.normalize('/path/to/project/api/src/functions/foo/foo.scenarios.js')
]
).toMatchSnapshot('Scenario snapshot')
})

test('creates a multi word function file', () => {
expect(
multiWordDefaultFiles[
path.normalize('/path/to/project/api/src/functions/sendMail.js')
path.normalize('/path/to/project/api/src/functions/sendMail/sendMail.js')
]
).toMatchSnapshot()
})

test('creates a .js file if --javascript=true', () => {
expect(
javascriptFiles[
path.normalize('/path/to/project/api/src/functions/javascriptFunction.js')
path.normalize(
'/path/to/project/api/src/functions/javascriptFunction/javascriptFunction.js'
)
]
).toMatchSnapshot()
// ^ JS-function-args should be stripped of their types and consequently the unused 'aws-lamda' import removed.
// https://babeljs.io/docs/en/babel-plugin-transform-typescript
})

test('creates a .ts file if --typescript=true', () => {
const fileNames = Object.keys(typescriptFiles)
expect(fileNames.length).toEqual(3)

expect(fileNames).toEqual(
expect.arrayContaining([
expect.stringContaining('typescriptFunction.ts'),
expect.stringContaining('typescriptFunction.test.ts'),
expect.stringContaining('typescriptFunction.scenarios.ts'),
])
)

expect(
typescriptFiles[
path.normalize('/path/to/project/api/src/functions/typescriptFunction.ts')
path.normalize(
'/path/to/project/api/src/functions/typescriptFunction/typescriptFunction.ts'
)
]
).toMatchSnapshot()
// ^ TS-functions, on the other hand, retain the 'aws-lamda' import and type-declartions.
Expand Down
Loading

0 comments on commit 5c19235

Please sign in to comment.