Skip to content

Commit

Permalink
Add screenshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Nov 9, 2024
1 parent 7858ca2 commit 8ca44b1
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 69 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: Install Modules
run: npm ci

- name: Run Unit Tests
if: matrix.os == 'ubuntu-22.04'
run: xvfb-run --auto-servernum npm run test-ci
- uses: openrndr/[email protected]
- run: xvfb-run glxinfo

# - name: Run Unit Tests
# if: matrix.os == 'ubuntu-22.04'
# run: xvfb-run --auto-servernum npm run test-ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
node_modules/
examples/3d-core/*.jpg
*.log
test/__diff__
Binary file added __screenshots__/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 39 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@eslint/js": "^9.13.0",
"eslint": "^9.13.0",
"node-addon-api": "^8.2.1",
"pixelmatch": "^6.0.0",
"three": "0.170.0",
"typescript": "^5.6.3"
}
Expand Down
45 changes: 45 additions & 0 deletions test/compare.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('node:assert').strict;
const { describe, it } = require('node:test');
const three = require('three');

const { screenshot } = require('./screenshot');
const { window, document } = require('./init');


const loadBox = () => {
return new Promise((res) => {
let mesh = null;
const texture = new three.TextureLoader().load(
__dirname + '/../examples/three/textures/crate.gif',
() => res(mesh),
);
texture.colorSpace = three.SRGBColorSpace;
const geometry = new three.BoxGeometry();
const material = new three.MeshBasicMaterial({ map: texture });
mesh = new three.Mesh(geometry, material);
mesh.rotation.x = Math.PI / 7;
mesh.rotation.y = Math.PI / 5;
});
};

describe('Screenshots', () => {
it('matches box screenshot', async () => {
const camera = new three.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 2;
const scene = new three.Scene();

const mesh = await loadBox();
scene.add(mesh);

const renderer = new three.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

renderer.render(scene, camera);

assert.ok(await screenshot('box'));
});
});
53 changes: 2 additions & 51 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

const assert = require('node:assert').strict;
const { describe, it } = require('node:test');

const three = require('three');
const { init } = require('..');

const inited = init();
const inited = require('./init');
const {
gl, Document, Window, Image,
Brush, Cloud, Drawable, Lines, Points, Rect, Screen, Surface, Tris,
gl, Brush, Cloud, Drawable, Lines, Points, Rect, Screen, Surface, Tris,
} = inited;


Expand Down Expand Up @@ -117,33 +114,6 @@ const staticClasses = {
},
};


const initedClasses = {
Image: {
create() {
return new Image();
},
props: ['src'],
methods: ['on'],
},

Window: {
create() {
return new Window();
},
props: ['size'],
methods: ['show'],
},

Document: {
create() {
return new Document();
},
props: ['body'],
methods: ['createElement'],
},
};

describe('Node.js 3D Core', () => {
it('exports an object', () => {
assert.strictEqual(typeof inited, 'object');
Expand Down Expand Up @@ -181,23 +151,4 @@ describe('Node.js 3D Core', () => {
});
}));
});

describe('Inited classes', () => {
Object.keys(initedClasses).forEach(
(c) => {
it(`${c} is exported`, () => {
assert.strictEqual(typeof inited[c], 'function');
});
}
);

Object.keys(initedClasses).forEach((c) => describe(c, () => {
const current = initedClasses[c];
const instance = current.create();

it('can be created', () => {
assert.ok(instance instanceof inited[c]);
});
}));
});
});
11 changes: 11 additions & 0 deletions test/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const three = require('three');

const { init, addThreeHelpers } = require('..');

const inited = init({
isGles3: true,
isWebGL2: true,
});
addThreeHelpers(three, inited.gl);

module.exports = inited;
Loading

0 comments on commit 8ca44b1

Please sign in to comment.