Skip to content

Instantly share code, notes, and snippets.

@gre
Last active September 24, 2016 09:40
Show Gist options
  • Save gre/94871edb6927e9c8b6fc9754e650e7da to your computer and use it in GitHub Desktop.
Save gre/94871edb6927e9c8b6fc9754e650e7da to your computer and use it in GitHub Desktop.
const { transform, execute } = require("./lib");
const ReactNativeWeb = require("react-native-web");
const {render} = require("react-dom");
const example = `
...jestsnapshotcodegoeshere
`;
const out = transform(example);
const node = execute(out, {
components: ReactNativeWeb,
});
const container = document.createElement("div");
container.style.margin = "20px auto";
container.style.width = "500px";
document.body.appendChild(container);
render(node, container);
const React = require("react");
const babelCore = require("babel-core");
exports.transform = (snapshot) => {
const code =
snapshot
.replace(/Object {/g, "{")
.replace(/Array \[/g, "[")
.replace(/\[Function .*\]/g, "()=>{}");
return babelCore.transform(code, {
plugins: [require("babel-plugin-transform-react-jsx")]
});
};
exports.execute = (out, opts = {}) => {
const ctx = Object.assign({
React,
}, opts.components);
with (ctx) {
return eval(out.code);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment