GithubHelp home page GithubHelp logo

test-tools's Introduction

VTEX Test Tools

Npm badge CI

Add tests to your VTEX IO app in an instant 🚀

Table of Contents

Install

yarn add -D @vtex/test-tools @apollo/react-testing@3 react-intl@3

Usage

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

{
  "name": "my-io-app",
  "scripts": {
    "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 @testing-library/react (RTL) under the hood, so most of its API is the same (read their docs here).

We added a few more features to the regular render function from RTL, such as a graphql and locale option. You can see more about them down below.

React Hooks

You can also test your custom hooks.

Example

import { hooks } from '@vtex/test-tools/react'
import useCustomHook from './useCustomHook'

const { renderHook, act } = hooks

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(() => 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

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:

{
  "name": "my-awesome-io-app",
  "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.

You can pass your mocked queries to it using the graphql option. Example:

import React from 'react'
import { render, flushPromises } from '@vtex/test-tools/react'
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] },
  })

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

  await flushPromises()
  jest.runAllTimers()

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

End-to-end flows

We offer hooks to make writing end-to-end tests easier.

Extended commands:

  • clickById
  • getById
  • typeById

Custom methods:

  • goToSearchPage
  • goToProductPageByShelf
  • checkText
  • checkIfElementExists
  • openCart
  • closeCart
  • clearCart
  • fillAndCheckShippingSimulator
  • scrollToId

Example:

import { openCart } from "@vtex/test-tools/cypress"

describe('Searchbar', () => {
  before(() => {
    cy.intercept(/operationName=ProductsSuggestionsQuery/, {
      fixture: 'product-suggestions-query.json',
    }).as('searchSuggestionsLoad')
  })

  it('finds a very specific product using the search bar', () => {
    cy.visit('/')

    cy.getById('searchBarInput').eq(0).type('Product')

    cy.getById('searchSuggestionsItem').contains('Product').click()

    cy.clickById('addToCart')

    openCart()

    ...
  })
})

You can check more details of these hooks in the links below:

Examples

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

License

MIT © VTEX

test-tools's People

Contributors

bentoper avatar dependabot[bot] avatar gustavoma avatar jgfidelis avatar kaisermann avatar klzns avatar lucasecdb avatar maik3345 avatar malvadeza avatar paul0vinicius avatar salesfelipe avatar thiagomurakami avatar victorhmp avatar viniciuslagedo avatar vitorflg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

test-tools's Issues

How can i ignore module vtexjs?

Since vtexjs is a native module in javascript, i'm trying to test the checkout with checkout-ui-cusotm but it tells me that vtexjs is undefined and orderFormId is undefined, so how can I test with all the vtexjs package included

image

Locale is not working to test different different languages

These two approaches:

"vtexTestTools": {
    "defaultLocale": "pt"
  },

or

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

  const element = getByText(/Olá!/)

  expect(element).toBeDefined()
})

are not working as expected and they are falling into the default language (english)

Make Jest export global

Find a way to export Jest type definitions in a global way, kinda like create-react-app does it, so that the user does not need to import @vtex/test-tools/react in order to use Jest functions without TypeScript complaining.

Test not find module vtex.css-handles

when i has an import from 'vtex.css-handles' in my code, test print this error:
image

How to do a test-tool resolve 'vtex.css-handles' or mock this module?

Node 12 not supported by jest

Hi,
unfortunately jest update to version 29 kicked support for node 12.
Trying to link (or publish/deploy) a vtex-app with vtex/test-tools dependency now fails with
image
Only workaround so far is to remove vtex/test-tools and jest completly from package.json, deleting node_modules and reinstall (removing jest from tsconfig also)

So vtex apps can't be tested anymore!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.