Giter Club home page Giter Club logo

marko-tester's Introduction

Build Status Test Coverage

marko-tester

A utility that helps you test marko components within jest framework.

Requirements

Your project needs to have jest@^23 and marko@^4.13 (with marko-widgets@^7, if legacy components support is needed) installed.

Within your regular jest config, you need to specify a transform for *.marko files (Note: if your NODE_ENV is set to dev or development, you need to run jest with "cache": false option):

...
"cache": false, // Only if your NODE_ENV is either `dev` or `development`.
"transform": {
  ...
  "\\.marko$": "<rootDir>/node_modules/marko-tester/preprocessor.js"
}

You can also test projects with marko@^3.14 and marko-widgets@^6.6. In this scenario always disable jest's cache.

Configuration

You can set up tester in the "global" section of jest's config:

"globals": {
  ...
  "tester": {
    "shallow": false
  }
}
  • shallow (default: true) - You can turn off shallow rendering by passing false here. That way marko won't isolate any component test.

Usage

marko-tester returns a function for you to use. Pass a relative path to a marko component file and you will receive a render method and fixtures method/object. By default this will run JEST SnapShots test for the fixtures of the component.

  • render - a method that renders a component with a given input and mounts it to document.body. The mounted component instance is returned.

  • fixtures - an object that contains all fixtures that are found within the __snapshots__ folder for the component. You can get content of a fixture by specifying a filename: fixtures[FixtureFileName].

As a second argument you can pass options object:

  • withoutFixtures (default: false) - set this to true if you don't want automatic snapshot test execution. At this point fixtures will become executable and you will be able to run snapshot test using fixtures(). You can run specific snapshot by providing a fixture name fixtures(FixtureFileName).

  • withAwait (default: false) - if your template has <await/> tag in it, you need to set this to true. At this point render will become an asynchronous function and you will need to treat it with await.

Examples

const { render, fixtures } = require('marko-tester')('../index.marko');

describe('When component is rendered without results', () => {
  let component;

  beforeEach(() => {
    component = render(fixtures['without-results']);
  });

  afterEach(() => {
    component.destroy();
  });

  ...your assertions...
});

Without fixtures:

const { render, fixtures } = require('marko-tester')('../index.marko', { withoutFixtures: true });

fixtures();

describe('When component is rendered with records', () => {
  let component;

  beforeEach(() => {
    component = render(fixtures.records);
  });

  afterEach(() => {
    component.destroy();
  });

  ...your assertions...
});

Asynchronous:

const { render, fixtures } = require('marko-tester')('../index.marko', { withAwait: true });

describe('When component is rendered without model', () => {
  let component;

  beforeEach(async () => {
    component = await render(fixtures.empty);
  });

  afterEach(() => {
    component.destroy();
  });

  ...your assertions...
});

If you just want to have snapshot test:

require('marko-tester')('../index.marko');

You can find more examples in the tests folder.

References

Thanks

Licence

MIT

marko-tester's People

Contributors

oxala avatar semantic-release-bot avatar scho7623 avatar phoenix727 avatar abiyasa avatar sdepold avatar darkwebdev avatar

Watchers

James Cloos avatar Dylan Piercey avatar  avatar

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.