Skip to content

Commit f41aae7

Browse files
committed
fix: exposed marko-tester as a function
1 parent fdd31f9 commit f41aae7

File tree

8 files changed

+64
-77
lines changed

8 files changed

+64
-77
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@ In the global JEST object, you can pass a `tester` configuration:
3232
- shallow - You can turn off shallow rendering by passing `false` here. That way marko won't isolate any component test. _(Default: true)_
3333

3434
## Usage
35-
`marko-tester` exposes a `getComponent` method for you to use. Pass a relative test file path to a marko component and you will receive a `render` method and `fixtures` method/object. By default, `getComponent` will run JEST SnapShot tests for the fixtures of the component.
35+
`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.
3636

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

39-
- `fixtures` is an object by default. It contains all the fixtures that are found within the fixture folder of this component. If a `withoutFixtures` option is passed to the `getComponent` method, `fixtures` will be a method that will run JEST SnapShot tests for your fixture. You will still be able to get fixture content by the filename: `fixtures[FixtureFileName]`.
39+
- `fixtures` is an object by default. It contains all the fixtures that are found within the fixture folder of this component. If a `withoutFixtures` option is passed, `fixtures` will be a function that will run JEST SnapShots test for your fixtures. You will still be able to get any fixture content by the filename: `fixtures[FixtureFileName]`.
4040

4141
### Example
4242
You can find examples in the `tests` folder. The boilerplate looks like this:
4343

4444
```
45-
const { getComponent } = require('marko-tester');
46-
const { render } = getComponent('../index.marko');
45+
const { render } = require('marko-tester')('../index.marko');
4746
4847
describe('When component is rendered', () => {
4948
let component;

src/index.js

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,75 +33,73 @@ const getFullPath = (componentPath) => {
3333
return index > -1 && resolve(stack[index].getFileName(), '..', componentPath);
3434
};
3535

36-
module.exports = {
37-
getComponent: (componentPath, { withoutFixtures } = {}) => {
38-
const fullPath = getFullPath(componentPath);
36+
module.exports = (componentPath, { withoutFixtures } = {}) => {
37+
const fullPath = getFullPath(componentPath);
3938

40-
if (!fullPath) {
41-
throw new Error(`Cannot find specified component at "${componentPath}".`);
42-
}
43-
44-
function render(input) {
45-
/* eslint-disable-next-line global-require, import/no-dynamic-require */
46-
const component = require(fullPath);
39+
if (!fullPath) {
40+
throw new Error(`Cannot find specified component at "${componentPath}".`);
41+
}
4742

48-
/* eslint-disable-next-line global-require, import/no-unresolved */
49-
require('marko/components').init();
43+
function render(input) {
44+
/* eslint-disable-next-line global-require, import/no-dynamic-require */
45+
const component = require(fullPath);
5046

51-
jest.resetModules();
47+
/* eslint-disable-next-line global-require, import/no-unresolved */
48+
require('marko/components').init();
5249

53-
return component
54-
.renderSync(clone(input))
55-
.appendTo(document.body)
56-
.getComponent();
57-
}
50+
jest.resetModules();
5851

59-
let fixtures = {};
60-
const fixturesPath = getFullPath(global.tester.fixturesDir);
61-
const runFixtures = () => {
62-
if (fixturesPath) {
63-
let fixturesAmount = 0;
52+
return component
53+
.renderSync(clone(input))
54+
.appendTo(document.body)
55+
.getComponent();
56+
}
6457

65-
readdirSync(fixturesPath).forEach((file) => {
66-
const absPath = join(fixturesPath, file);
67-
const extension = extname(absPath);
68-
const testName = basename(absPath).replace(/\.(js|json)$/, '');
58+
let fixtures = {};
59+
const fixturesPath = getFullPath(global.tester.fixturesDir);
60+
const runFixtures = () => {
61+
if (fixturesPath) {
62+
let fixturesAmount = 0;
6963

70-
if (extension === '.js' || extension === '.json') {
71-
fixturesAmount += 1;
64+
readdirSync(fixturesPath).forEach((file) => {
65+
const absPath = join(fixturesPath, file);
66+
const extension = extname(absPath);
67+
const testName = basename(absPath).replace(/\.(js|json)$/, '');
7268

73-
let fixture;
69+
if (extension === '.js' || extension === '.json') {
70+
fixturesAmount += 1;
7471

75-
try {
76-
/* eslint-disable-next-line global-require, import/no-dynamic-require */
77-
fixture = require(absPath);
78-
} catch (error) { /* */ }
72+
let fixture;
7973

80-
fixtures[testName] = fixture || {};
74+
try {
75+
/* eslint-disable-next-line global-require, import/no-dynamic-require */
76+
fixture = require(absPath);
77+
} catch (error) { /* */ }
8178

82-
it(`should render component with ${testName} fixture`, () => {
83-
const comp = render(clone(fixture));
84-
expect(Array.from(document.body.childNodes)).toMatchSnapshot();
85-
comp.destroy();
86-
});
87-
}
88-
});
79+
fixtures[testName] = fixture || {};
8980

90-
if (fixturesAmount === 0) {
91-
throw new Error(`No fixtures where found for component in "${fullPath}".`);
81+
it(`should render component with ${testName} fixture`, () => {
82+
const comp = render(clone(fixture));
83+
expect(Array.from(document.body.childNodes)).toMatchSnapshot();
84+
comp.destroy();
85+
});
9286
}
93-
}
94-
};
87+
});
9588

96-
if (withoutFixtures) {
97-
fixtures = runFixtures;
98-
} else {
99-
runFixtures();
89+
if (fixturesAmount === 0) {
90+
throw new Error(`No fixtures where found for component in "${fullPath}".`);
91+
}
10092
}
101-
102-
return {
103-
fixtures,
104-
render,
105-
};
106-
},
93+
};
94+
95+
if (withoutFixtures) {
96+
fixtures = runFixtures;
97+
} else {
98+
runFixtures();
99+
}
100+
101+
return {
102+
fixtures,
103+
render,
104+
};
107105
};

tests/components/component/test/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const { getComponent } = require('../../../../src/index');
2-
3-
const { render, fixtures } = getComponent('../index.marko', { withoutFixtures: true });
1+
const { render, fixtures } = require('../../../../src/index')('../index.marko', { withoutFixtures: true });
42

53
fixtures();
64

tests/components/legacy/test/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const { getComponent } = require('../../../../src/index');
2-
3-
const { render, fixtures } = getComponent('../component.js');
1+
const { render, fixtures } = require('../../../../src/index')('../component.js');
42

53
beforeEach(() => {
64
window.alert = jest.fn();

tests/components/no-fixtures/test/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const { getComponent } = require('../../../../src/index');
2-
3-
const { render } = getComponent('../index.marko');
1+
const { render } = require('../../../../src/index')('../index.marko');
42

53
describe('When component is rendered', () => {
64
let component;

tests/components/tbody-component/test/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const { getComponent } = require('../../../../src/index');
2-
3-
const { render, fixtures } = getComponent('../index.marko');
1+
const { render, fixtures } = require('../../../../src/index')('../index.marko');
42

53
describe('When component is rendered', () => {
64
let component;

tests/components/zero-fixtures/test/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { resolve } = require('path');
2-
const { getComponent } = require('../../../../src/index');
3-
4-
const { fixtures } = getComponent('../index.marko', { withoutFixtures: true });
2+
const { fixtures } = require('../../../../src/index')('../index.marko', { withoutFixtures: true });
53

64
it('should throw an error abount empty fixtures folder', () => {
75
expect(() => fixtures()).toThrowError(`No fixtures where found for component in "${resolve(__dirname, '../index.marko')}".`);

tests/no-component/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { getComponent } = require('../../src/index');
1+
const markoTester = require('../../src/index');
22

33
it('should throw an error when no component is found', () => {
4-
expect(() => getComponent('./index.marko')).toThrowError('Cannot find specified component at "./index.marko".');
4+
expect(() => markoTester('./index.marko')).toThrowError('Cannot find specified component at "./index.marko".');
55
});

0 commit comments

Comments
 (0)