Skip to content

Commit

Permalink
Add example of schema validation test with Joi
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanteixeira committed Aug 24, 2022
1 parent c90616a commit 5ed5cce
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
19 changes: 19 additions & 0 deletions lib/expect-schema.js
@@ -0,0 +1,19 @@
const toMatchSchema = (received, schema) => {
const { error } = schema.validate(received, { presence: 'required' })
const pass = !error

if (pass) {
return {
pass: true
}
}

return {
message: () => `${error}`,
pass: false
}
}

module.exports = {
toMatchSchema
}
13 changes: 13 additions & 0 deletions lib/schemas.js
@@ -0,0 +1,13 @@
const Joi = require('joi')

const PRODUCT_SCHEMA = Joi.object({
nome: Joi.string(),
preco: Joi.number(),
descricao: Joi.string(),
quantidade: Joi.number(),
_id: Joi.string()
})

module.exports = {
PRODUCT_SCHEMA
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -14,11 +14,11 @@
"axe-playwright": "^1.1.11",
"eslint": "^8.15.0",
"eslint-plugin-playwright": "^0.9.0",
"expect-playwright": "^0.8.0",
"happo-e2e": "^1.2.0",
"happo-playwright": "^1.1.0",
"happo.io": "^7.2.1",
"http-status-codes": "^2.2.0",
"joi": "^17.6.0",
"playwright": "^1.22.1",
"serverest": "2.26.2",
"wait-on": "^6.0.1"
Expand Down
4 changes: 2 additions & 2 deletions playwright.config.js
@@ -1,7 +1,7 @@
const { matchers } = require('expect-playwright')
const { expect } = require('@playwright/test')
const toMatchSchema = require('./lib/expect-schema')

expect.extend(matchers)
expect.extend(toMatchSchema)

process.env.PLAYWRIGHT_EXPERIMENTAL_FEATURES = '1'

Expand Down
9 changes: 9 additions & 0 deletions tests/api/product.api.test.js
Expand Up @@ -9,6 +9,7 @@ const {
PRODUCT_NOT_FOUND
} = require('serverest/src/utils/constants')
const { getAuthToken, getProductBody, getProductId, getUserBody } = require('../../lib/helpers')
const { PRODUCT_SCHEMA } = require('../../lib/schemas')

test.describe.parallel('Product API', () => {
let authorization
Expand Down Expand Up @@ -56,6 +57,14 @@ test.describe.parallel('Product API', () => {
expect(response.status()).toEqual(StatusCodes.OK)
})

test('validates product schema', async ({ request }) => {
const _id = await getProductId(request, authorization, getProductBody())
const response = await request.get(`/produtos/${_id}`)
const product = await response.json()

expect(product).toMatchSchema(PRODUCT_SCHEMA)
})

test('fails to retrieve a product when it does not exist', async ({ request }) => {
const _id = getUserBody().password
const response = await request.get(`/produtos/${_id}`)
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Expand Up @@ -1210,11 +1210,6 @@ event-loop-stats@1.2.0:
dependencies:
nan "^2.14.0"

expect-playwright@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/expect-playwright/-/expect-playwright-0.8.0.tgz#6d4ebe0bdbdd3c1693d880d97153b96a129ae4e8"
integrity sha512-+kn8561vHAY+dt+0gMqqj1oY+g5xWrsuGMk4QGxotT2WS545nVqqjs37z6hrYfIuucwqthzwJfCJUEYqixyljg==

express-async-errors@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/express-async-errors/-/express-async-errors-3.1.1.tgz#6053236d61d21ddef4892d6bd1d736889fc9da41"
Expand Down

0 comments on commit 5ed5cce

Please sign in to comment.