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

Recommendation how to snapshot custom blocks with jest #13364

Closed
ferdiesletering opened this issue Jan 17, 2019 · 8 comments
Closed

Recommendation how to snapshot custom blocks with jest #13364

ferdiesletering opened this issue Jan 17, 2019 · 8 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@ferdiesletering
Copy link
Contributor

ferdiesletering commented Jan 17, 2019

I want to snapshot my custom blocks with jest. I found out how gutenberg is testing its components inside the block library, and I want to do it the same way.

So I have copied this helper to my project and set up a basic block.

However when I run npm test the following error is shown:

When I run npm test the following error is shown

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import EventedTokenizer from './evented-tokenizer';
                                                                                                    ^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (../../../../../../../riad/Workspace/a8c/gutenberg/packages/blocks/src/api/validation.js:4:1)

Block

// block.js
export const name = 'myblock';

export const settings = {
	title: 'title',
	description: 'title',
	icon: {
		background: '#0293b0',
		foreground: '#fff',
		src: 'format-aside',
	},
	edit: () => ( <div>Edit</div> ),
	save: () => ( <div>Save</div> ),
};

Test

// tests/index.js
import { blockEditRender } from '../../../tests/helpers';
import { settings, name } from '../';

it( 'renders correctly when there are no items', () => {
	const wrapper = blockEditRender( name, settings );
        expect( wrapper ).toMatchSnapshot();
} );

I included this package inside my package.json

"@wordpress/blocks": "^6.0.6",

Is there a recommended guide to snapshot custom gutenberg blocks?
I've read the testing guideline https://wordpress.org/gutenberg/handbook/contributors/testing-overview/, but it explains testing more in general then testing a specific gutenberg block with its components.

Thank you

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Jan 18, 2019
@youknowriad
Copy link
Contributor

Thanks for opening the issue.

I think @Shelob9 had a tutorial somewhere about unit testing Gutenberg blocks.
Alternatively, you can use e2e tests which catch more regressions.

I think this is an area where the community should experiment and figure out the guidelines for the WordPress project.

@Shelob9
Copy link
Contributor

Shelob9 commented Jan 18, 2019

I did write a few on testing (Jest/Enzyme) for React with WordPress devs in mind:

Here is how we use Enzyme to test that our block shows up in the block inserter: https://github.com/CalderaWP/Caldera-Forms/blob/develop/cypress/integration/test.test.js#L46-L67

I am working on more specifically on blocks, but short version -- my opinion is how the block works is WordPress' concern. If the components you use to assemble the block are totally tested, you can assume setAttributes() works.

@iandunn
Copy link
Member

iandunn commented Apr 10, 2019

Related WordPress/gutenberg-examples#26

@nerrad
Copy link
Contributor

nerrad commented Apr 26, 2019

I started encountering this issue today when I refactored a block from importing InspectorControls from @wordpress/editor to @wordpress/block-editor (ya really odd right?).

Anyways, there is a workaround. I pointed the simple-html-token module to a mock via my jest-config:

(just showing the moduleNameMapper key in the json for the config below.

{
   "moduleNameMapper": {
        "simple-html-token": "<rootDir>/path/to/mock/simple-html-token"
   }
}

Then in the mock file ( /path/to/mock/simple-html-token.js) do this:

module.exports = jest.fn();

@nerrad
Copy link
Contributor

nerrad commented Apr 26, 2019

I'm guessing the issue here is that the import is coming from a specific es6 directory from the simple-html-tokenizer module:

import Tokenizer from 'simple-html-tokenizer/dist/es6/tokenizer';

But I still don't get how/why this got triggered by switching from importing @wordpress/block-editor instead of @wordpress/editor unless somehow using the proxies (in this case for InspectorControls) affects how things get transformed.

@gziolo
Copy link
Member

gziolo commented Apr 27, 2019

This discussion reminds me that we have this line added to Jest config:
https://github.com/WordPress/gutenberg/blob/master/test/unit/jest.config.js#L29

I don’t quite understand why it was added but it should be related.

@nerrad
Copy link
Contributor

nerrad commented Apr 27, 2019

ooo ya that would be it! I'm guessing it was added for the same reason we're seeing this issue (to ignore that transform in tests). In other words if you remove it from the gb jest config the same error will probably pop up for gb. I just verified adding that line also fixes the issue.

So likely the issue here is because simple-html-tokenizer does not have a published transpiled version for jest to use so this allows tests to just ignore it.

Since this will also affect plugins that test with imported wp packages, should this ignore pattern be added to the @wordpress/jest-preset-default package? At a minimum, maybe update the testing-overview docs?

@gziolo
Copy link
Member

gziolo commented Apr 29, 2019

Since this will also affect plugins that test with imported wp packages, should this ignore pattern be added to the @wordpress/jest-preset-default package? At a minimum, maybe update the testing-overview docs?

I opened #15246 which tries to fix it for good with code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

6 participants