代码之家  ›  专栏  ›  技术社区  ›  Alexander Staroselsky

Nuxt Ava端到端测试存储配置

  •  3
  • Alexander Staroselsky  · 技术社区  · 6 年前

    example

    import test from 'ava'
    import { Nuxt, Builder } from 'nuxt'
    import { resolve } from 'path'
    
    // We keep a reference to Nuxt so we can close
    // the server at the end of the test
    let nuxt = null
    
    // Init Nuxt.js and start listening on localhost:4000
    test.before('Init Nuxt.js', async t => {
      const rootDir = resolve(__dirname, '..')
      let config = {}
      try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
      config.rootDir = rootDir // project folder
      config.dev = false // production build
      config.mode = 'universal' // Isomorphic application
      nuxt = new Nuxt(config)
      await new Builder(nuxt).build()
      nuxt.listen(4000, 'localhost')
    })
    
    // Example of testing only generated html
    test('Route / exits and render HTML', async t => {
      let context = {}
      const { html } = await nuxt.renderRoute('/', context)
      t.true(html.includes('<h1 class="red">Hello world!</h1>'))
    })
    
    // Close the Nuxt server
    test.after('Closing server', t => {
      nuxt.close()
    })
    

    如何使用 Nuxt Builder store ? 示例Vuex商店如下所示:

    import Vuex from "vuex";
    
    const createStore = () => {
      return new Vuex.Store({
        state: () => ({
          todo: null
        }),
        mutations: {
          receiveTodo(state, todo) {
            state.todo = todo;
          }
        },
        actions: {
          async nuxtServerInit({ commit }, { app }) {
            console.log(app);
            const todo = await app.$axios.$get(
              "https://jsonplaceholder.typicode.com/todos/1"
            );
            commit("receiveTodo", todo);
          }
        }
      });
    };
    
    export default createStore;
    

    @nuxtjs/axios 方法$get:

    TypeError {
      message: 'Cannot read property \'$get\' of undefined',
    }
    

    我可以嘲笑你 $get $axios 提供于 app 在Vuex存储方法中 nuxtServerInit ,我只需要了解如何访问 应用程序 在测试配置中。

    0 回复  |  直到 6 年前