Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vtex/test-tools

Package Overview
Dependencies
Maintainers
57
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vtex/test-tools

VTEX IO testing tools

  • 3.1.0-beta.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8K
decreased by-46.78%
Maintainers
57
Weekly downloads
 
Created
Source

VTEX Test Tools

Npm badge

Add tests to your VTEX IO app in an instant 🚀

Install

$ yarn add @vtex/test-tools apollo-client apollo-cache-inmemory @apollo/react-testing react-intl graphql @types/jest @types/node @types/react @types/react-intl -D

Usage

Add a new script to your react/package.json:

  "test": "vtex-test-tools test"

Add these lines to your .vtexignore:

react/**/__tests__/**
react/**/__mocks__/**
react/**/__snapshots__/**
react/**/*.test.js
react/**/*.test.ts
react/**/*.test.tsx

Run in your terminal:

$ yarn test

If you're using TypeScript there are a few more steps.

Done! 🎉

Under the hood, we use Jest, so you can pass Jest flags as parameters: read the docs.

API

react module

The module react makes it easy to test VTEX IO React apps.

Example
import React from 'react'
import { render } from '@vtex/test-tools/react'
import HelloWorld from './HelloWorld'

test('should render the Hello!', () => {
  const { getByText } = render(<HelloWorld />)

  const element = getByText(/Hello!/)

  expect(element).toBeDefined()
})

This module uses react-testing-library under the hood, so most of its API it's the same: read the docs.

There are few new features added to it:

React Hooks

You can also test your custom hooks.

Example
import { renderHook, act } from '@vtex/test-tools/react'
import useCustomHook from "./useCustomHook"

it('counter should be one', async () => {
  const { result } = renderHook(() => useCustomHook())

  // This waits for the useEffect hook to be triggered and mutate hook state
  await act(async () => await Promise.resolve())

  expect(result.current).toBe(1)
})

The module uses @react-testing-library/react-hooks under the hood, to understand the reactHook function you can read its doc

There are few new features added to it:

Messages

We will automatically wrap your component with an IntlProvider with your app's messages/en-US.json messages.

You can change the default locale being used adding a config to your package.json. Example:

  "vtexTestTools": {
    "defaultLocale": "pt-BR"
  }

If you want to change the locale just in a test, you may pass the locale option. Example:

import React from 'react'
import { render } from '@vtex/test-tools/react'
import HelloWorld from './HelloWorld'

test('should render the example translated to portuguese', () => {
  const { getByText } = render(
    <HelloWorld />,
    { locale: 'pt' }
  )

  const element = getByText(/Olá!/)

  expect(element).toBeDefined()
})

GraphQL

We automatically wrap your component with an Apollo's MockedProvider. Just import it from @apollo/react-testing and pass it as the MockedProvider option.

You can pass props to it using the graphql option. Example:

import React from 'react'
import { render, flushPromises } from '@vtex/test-tools/react'
import { MockedProvider } from '@apollo/react-testing'
import GraphqlComponent from './GraphqlComponent'
import GET_BOOKS from './getBooks.graphql'

test('should render mock graphql responses', async () => {
  jest.useFakeTimers()

  const bookMock = {
    request: {
      query: GET_BOOKS,
    },
    result: {
      data: {
        books: [
          {
            id: 10,
            title: 'Hello',
          }
        ]
      }
    }
  }

  const { getByText } = render(<GraphqlComponent />, {
    graphql: { mocks: [bookMock] },
    MockedProvider,
  })

  expect(getByText(/Loading/)).toBeDefined()

  await flushPromises()
  jest.runAllTimers()

  expect(getByText(/Hello/)).toBeDefined()
})

Examples

These are some common use cases that might be helpful to see how it's done:

Debugging tests in VS Code

Testing a React component

Testing a React Hook

TypeScript

Testing a component using CSS Modules

Testing a component using GraphQL

Testing a component using React Hooks

Testing a component translating in another locale

License

MIT © VTEX

Keywords

FAQs

Package last updated on 17 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc