Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(template): refactor and split code for test helper #317

Closed

Conversation

BibiSebi
Copy link
Contributor

@BibiSebi BibiSebi commented Nov 14, 2023

What?

In this PR, I have refactored and separated the test helper PoC code into a separate file. I used the first approach #307 as it was easier to split.

Once the PR #313 is merged, I will continue with the following next steps:

  • 1. Move test utility function into its own package under packages/helpers/test
  • 2. Add the test inside field-plugin exports in package.json
  • 3. Publish new field-plugin version
  • 4. Create tests for react, vue 3 and vue 2 templates with the new field-plugin version
  • 5. Add a new CI step to run tests after the build

Why?

EXT-2078

How to test? (optional)

  1. Run yarn install
  2. Run yarn test:template react

Copy link

vercel bot commented Nov 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plugin-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 20, 2023 1:20pm

@BibiSebi
Copy link
Contributor Author

BibiSebi commented Nov 14, 2023

}

describe('FieldPluginExammple', () => {
describe('FieldPluginExample', () => {
afterEach(() => {
vi.unstubAllGlobals()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After working closely on the test helper, I realized that we will need to provide additional documentation, maybe a comment that explains that vi.unstubAllGlobals() is needed if they use our helper setup() function. This at first glance might be rather unintuitive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Maybe we could do

test('something', async () => {
  const { cleanUp } = setupFieldPlugin();
  
  // do stuff

  cleanUp();
});

And cleanUp is actually just vi.unstubAllGlobals() internally. What do you think?

@BibiSebi BibiSebi requested a review from eunjae-lee November 20, 2023 13:18
@BibiSebi BibiSebi marked this pull request as ready for review November 20, 2023 13:18
@BibiSebi BibiSebi changed the title fix(template) - refactor and split code for test helper fix(template): refactor and split code for test helper Nov 20, 2023
Copy link
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I see vi.mock can be tricky to abstract because of hoisting. I guess if we still took the approach #2, we could've done something like..

// something.spec.tsx
import { mockedLibrary } from '@storyblok/field-plugin/test'

vi.mock('@storyblok/field-plugin', mockedLibrary());

meaning it's the user who needs to mock the library while we provide the mock implementation. However, the approach #1 (the current PR) seems cleaner for users than that 🙂

}

describe('FieldPluginExammple', () => {
describe('FieldPluginExample', () => {
afterEach(() => {
vi.unstubAllGlobals()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Maybe we could do

test('something', async () => {
  const { cleanUp } = setupFieldPlugin();
  
  // do stuff

  cleanUp();
});

And cleanUp is actually just vi.unstubAllGlobals() internally. What do you think?

@eunjae-lee eunjae-lee closed this Nov 28, 2023
@BibiSebi BibiSebi deleted the chore/testing-helper-move-package branch February 8, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants