Nuxt Ava端到端测试存储配置

问题描述 投票:4回答:1

给出使用Ava的示例官方Nuxt端到端测试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()
})

如何使用NuxtBuilder配置/访问应用程序Vuex 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;

当前正在尝试运行提供的Ava测试,导致尝试访问@nuxtjs/axios方法$ get时出错,>

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

我可以在Vuex存储方法$get中模拟$axios甚至app上可用的nuxtServerInit,我只需要了解如何在测试配置中访问app

感谢您提供的任何帮助。

给出了使用Ava的官方Nuxt端到端测试示例:从'ava'导入测试import {Nuxt,Builder}从'nuxt'import {resolve}从'path'// //我们保留对Nuxt的引用,因此我们可以...

vuejs2 vuex nuxt.js ava
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.