NervJS / nerv

A blazing fast React alternative, compatible with IE8 and React 16.
https://nerv.aotu.io
MIT License
5.42k stars 267 forks source link

jest testing Nerv is not defined #145

Closed zhanglong-3ti closed 4 years ago

zhanglong-3ti commented 4 years ago

测试代码

import Nerv, { findDOMNode } from 'nervjs';
import { Simulate, renderIntoDocument } from 'nerv-test-utils';

import { AccountEditPage } from './AccountEditPage';

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

describe('<AccountEditPage />', () => {
   it('should render correctly', async () => {
        const component = renderIntoDocument(<AccountEditPage />);
        await delay(300);
        const items = findDOMNode(component, 'list-container').querySelectorAll('.list-item');
        const item0 = items[0];
        Simulate.click(item0);
        // expect(onClick).not.toBeCalled()
    });
});

转换成H5进行测试之后的代码:

import Taro from "@tarojs/taro-h5";
/* eslint-disable react/no-find-dom-node */
import { findDOMNode } from 'nervjs';
import { Simulate, renderIntoDocument } from 'nerv-test-utils';
import { AccountEditPage } from './AccountEditPage';
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
describe('<AccountEditPage />', () => {

  it('should render correctly', async () => {
    const component = renderIntoDocument(<AccountEditPage />);
    await delay(300);
    const items = findDOMNode(component, 'list-container').querySelectorAll('.list-item');
    const item0 = items[0];
    Simulate.click(item0);
    // expect(onClick).not.toBeCalled()
  });
});

转换之后nervjs没把Nerv引入。

以下是我的babel

babel.config.js


const apis = require('@tarojs/taro-h5/dist/taroApis')

module.exports = { presets: [ [ '@babel/env', {

    spec: true,
    useBuiltIns: false,
  },
],

], plugins: [ '@babel/plugin-proposal-class-properties', [ '@babel/plugin-transform-react-jsx', { pragma: 'Nerv.createElement', }, ], ['@babel/plugin-proposal-object-rest-spread'], [ 'babel-plugin-transform-taroapi', { apis, packageName: '@tarojs/taro-h5', }, ] ], }

yuche commented 4 years ago

你可以在测试环境把 pragma 换成 Taro.createElement 试试

zhanglong-3ti commented 4 years ago

你可以在测试环境把 pragma 换成 Taro.createElement 试试

我使用了Nerv.findDOMNode,自动引入 Nerv就可以了