d
Yeah!!!
t
Can you tell me what you've done so far
so it will be easier for me to guide you
d
Hey yeah @thousands-gold-77695 I've set up Cypress following : https://docs.cypress.io/guides/tooling/typescript-support#Install-TypeScript
Whenever I import a file from my src that's in ts it errors out
t
OK, so for component testing you'll want to do the following
In your
cypress.json
you'll want to have this:
Copy code
json
{
  "component": {
    "componentFolder": "src",
    "supportFile": "cypress/support/support.tsx",
    "testFiles": "**/*.spec.{js,ts,jsx,tsx}"
  }
}
Are you using some library that needs to have global styles or you want to pass a provider? That's what I'm doing in support.tsx. I'm using Chakra-ui so I need to provide a theme provider
and I have this in
support.tsx
Copy code
ts
/// <reference types="cypress" />

import { ChakraProvider, Box } from '@chakra-ui/react'
import { mount } from '@cypress/react'
import customTheme from '@theme/customTheme'
import { Fonts } from '@theme/Fonts'
import { createElement, ReactElement, ReactNode } from 'react'

const ThemeProvider = ({ children }: { children: ReactNode }): ReactElement => {
  return (
    <ChakraProvider resetCSS={true} theme={customTheme}>
      <Fonts />
      <Box my={4} mx={6}>
        {children}
      </Box>
    </ChakraProvider>
  )
}

Cypress.Commands.add('mount', (component) => {
  mount(createElement(ThemeProvider, null, component))
})

Cypress.Commands.add('dataCy', (value) => {
  return cy.get(`[data-cy=${value}]`)
})
then in
package.json
you can simply add a script
"cypress:tc": "cypress open-ct"
d
I see, how do you get the cypress open-ct!
t
it's from Cypress
if you do
cypress open
it will open your end to end tests
if you do
cypress open-ct
itll will open your component tests
d
Oh I didn't know that, that's all you did to put together?
How does your index plugin
t
yes that's all I did to put it together.
in my plugin index file I have this:
Copy code
ts
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.ts can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

const injectNextDevServer = require('@cypress/react/plugins/next')
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
 * @type {Cypress.PluginConfig}
 */

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  require('@cypress/code-coverage/task')(on, config)
  if (config.testingType === 'component') {
    injectNextDevServer(on, config)
  }

  return config
}
But this is because I'm using code coverage
if you are not using code coverage then yo don't need
require('@cypress/code-coverage/task')(on, config)
d
I'll try this out, thank you so much I've been ripping my hair out over this
@User is cypress.json at root?
t
yes
d
@User support.tsx also. is this the commands ts or an entirely new files?
what does the nested cypress.json look like if you i may ask
t
no, support.ts is at cypress/support
it's a new file, commands.ts for me has different stuff
By the way, were you able to set up Component Testing with Cypress?
8 Views